Python 3#

Basics#

Data Types#

Data Type

Examples

Integers

-2, -1, 0, 1, 2, 3, 4, 5

Floating-point numbers

-1.25, -1.0, --0.5, 0.0, 0.5, 1.0, 1.25

Strings

'a', 'aa', 'aaa', 'Hello!', '11 cats'

Lists

['John', 'Peter', 'Debora', 'Charles']

Tuples

('table', 'chair', 'rack', 'shelf')

Dictionaries

{'color': 'red', 'age': 42}

Control Flow#

if statements#

if name != 'George':
    print('You are not George')
if name != 'George':
    print('You are not George')
else:
    print('You are George')
if name == 'Debora':
    print('Hi Debora!')
elif name == 'George':
    print('Hi George!')
else:
    print('Who are you?')

while Loop statements#

spam = 0
while spam < 5:
    print('Hello, world.')
    spam = spam + 1

for loop#

pets = ['Bella', 'Milo', 'Loki']
for pet in pets:
    print(pet)
for i in range(5):
    print(f'Will stop at 5! or 4? ({i})')

Operators#

Math Operators#

Operators

Operation

Example

**

Exponent

2 ** 3 = 8

%

Modulus/Remainder

22 % 8 = 6

//

Integer division

22 // 8 = 2

/

Division

22 / 8 = 2.75

*

Multiplication

3 * 3 = 9

-

Subtraction

5 - 2 = 3

+

Addition

2 + 2 = 4

Comparison Operators#

Operator

Meaning

==

Equal to

!=

Not equal to

<

Less than

>

Greater Than

<=

Less than or Equal to

>=

Greater than or Equal to

Boolean Operators#

Operator and#

Expression

Result

True and True

True

True and False

False

False and True

False

False and False

False

Operator or#

Expression

Result

True or True

True

True or False

True

False or True

True

False and False

False

Operator not#

Expression

Result

not True

False

not False

True