You are on page 1of 20

Basic coding in Python: part 3.

strazvan@jnu.ac.kr
Conditional Statements (if - else)

Choose a number for a and b

Always with indentation (obtained if you hit TAB key)

Boolean (true/false) values + Conditional statements (if/else)


Conditional Statements (if)

Always with indentation (obtained if you hit TAB key)

Hit twice
Note the indentation
ENTER
1. Hello World

2. Strings & Variables

3.Booleans

4. Data Collections & Functions

5. Loops
Booleans represent one of two values: True or False.

Rules
• Almost any value is True if it has some content
• Any string is True, except empty strings
• Any number is True, except 0
• Any list, tuple, set, and dictionary are True, except empty ones

• False values for empty fields, such as (), [], {}, "", the number 0, and the value None
• The value False evaluates to False.
Booleans represent one of two values: True or False.

Rules

• False values for empty fields, such as (), [], {}, "", the number 0, and the value None
• The value False evaluates to False.
Booleans represent one of two values: True or False.

Note the double == sign to indicate mathematical equality, different than when using variables (=)
Booleans represent one of two values: True or False.

Often, it is necessary to make conditional statements: if (X)….then (else, otherwise) (Y)…

Indentation (use TAB key) is Python's method for grouping statements


Logical Operators

• and: Returns True if both statements are true

• or: Returns True if one of the statements is true

• not: returns False if the result is true


Membership Operators

Membership operators are used to test if a sequence is presented in an object:

• in
• not in
Operator Precedence

Parentheses (expressions inside parentheses) are executed first:

Multiplication * has higher precedence than addition +

Addition + and subtraction - have the same precedence: execute commands from left to right:
Conditional Statements (if)

• 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


Conditional Statements (if)

• Python relies on indentation (whitespace at the beginning of a line) to define scope in the code.

• Other programming languages use curly-brackets for this purpose.

Repeat this exercise without indentation


Conditional Statements (if)

If you have only one statement to execute, you can put it on the same line as the if statement.
Conditional Statements ( nested if)

if statements inside if statements: nested if statements


Conditional Statements (elif)

• Elif: if the previous statement is not TRUE, execute following command


Conditional Statements (else)

• else: if the previous statements (using if or elif) are not TRUE, execute following command
• else can also be used without elif
Conditional Expressions

multiple else statements on the same line

Change the values for yourself


Conditional Expressions

Using operators: and or not

and

or

Not reverses the result of the conditional statement

not
Conditional Expressions
Nested if:
if statements inside other if statements

You might also like