You are on page 1of 17

Foundation Course in

Python
Escape Characters:
To insert characters that are illegal in a string, we use an escape character.
An escape character is a backslash(\) followed by the character you want to insert.

Example:\
You will get an error if you use double quotes inside a string that is surrounded by double quotes:

txt = "We are the so-called "Vikings" from the north."

The escape character allows you to use double quotes when you normally would not be allowed:

txt = "We are the so-called \"Vikings\" from the north."


Escape Characters:
\

\n = Newline feed

\t = Tab

\\ = Backslash
Python Conditional Operators:
 Equals: a == b
 Not Equals: a != b
 Less than: a < b
 Less than or equal to: a <= b
 Greater than: a > b
 Greater than or equal to: a >= b
Python Conditional Operators:

AND Operator – All conditions should be true.


OR Operator- Any one condition should be true.
Python Control Flow:
We often only want certain code to execute when a particular
condition has been met.

If, elif and else Statements


Python Control Flow:
● Control Flow syntax makes use of colons and indentation
(whitespace).

● This indentation system is crucial to Python and is what


sets it apart from other programming languages.
Python Control Flow:
● Python relies on indentation (whitespace at the beginning
of a line) to define scope in the code. Other programming
languages often use curly-brackets for this purpose.

if b > a:
print("b is greater than a") # you will get an error
Python Control Flow:
● Syntax of an if statement
if some_condition:
# execute some code
Python Control Flow:
● Syntax of an if/else statement
if some_condition:
# execute some code
else:
# do something else
Python Control Flow:
● Syntax of an if/else statement
if some_condition:
# execute some code
elif some_other_condition:
# do something different
else:
# do something else
Python Control Flow:
● Syntax of an if/else statement
if some_condition:
# execute some code
elif some_other_condition:
# do something different
else:
# do something else
Python Control Flow:
● Nested If Statement
if some_condition:
# execute some code
if some_other_condition:
# do something different
else:
# do something else
else:
#execute something else
Loops:

WHILE LOOP
While Loop:
While loops will continue to execute a block of code while some
condition remains True.
● Syntax of a while loop
while some_boolean_condition:
#do something
While Loop:
● You can combine with an else if you want
while some_boolean_condition: #do
something
else:
#do something different
While Loop:
● Break Statement: It will stop the loop even if while
condition is true.

● Continue Statement: It will stop current iteration and


continue with next.

You might also like