On this page
  1. python fundamentals
  2. Contents
  3. Statements
  4. Conditions & Control Flow
  5. if, elif, else
  6. while
  7. and and or
  8. pass
  9. is vs ==
  10. Operators
  11. Looping

python fundamentals

Prev: python3 | Up: python3 | Next: python functions
Rel: python3, __builtins__, python functions, Core Fundamentals, Syntax and Variables, Control Flow, isinstance, id
Ref: docs.python.org — The Python Tutorial
Tags: #public #python3 #fundamentals


Contents


Statements

Any individual line of executable code is a statement, such as variable assignment:

>>> x = 'some text'
>>> y = 12
>>> z = 3.4

Conditions & Control Flow

A conditional statement uses if, elif, else, or while, extended by and / or.

if, elif, else

>>> x = 'some text'
>>> if isinstance(x, int):
...     print('x is an integer.')
... elif isinstance(x, float):
...     print('x is a float.')
... elif isinstance(x, str):
...     print('x is a string.')
... else:
...     print('x is something else.')
x is a string.

while

>>> l = [1, 2, 3]
>>> while l:
...     print("curr list status:", l)
...     l.pop()
curr list status: [1, 2, 3]
3
curr list status: [1, 2]
2
curr list status: [1]
1

and and or

>>> x = 'some text'
>>> y = 2
>>> if isinstance(x, str) and y == 2:
...     print('this will print.')
this will print.

pass

Allows a block to do nothing without raising an IndentationError:

>>> if x:
...     pass

is vs ==

Where == returns True for equivalent values, is only returns True for the exact same object in memory ([[__builtins__/id|id()]]):

>>> x = 'some text'
>>> z = x
>>> x == z
True
>>> x is z
True
>>> id(x) == id(z)
True

Operators

>>> 2 + 2
4
>>> 4 ** 2
16
>>> 4 / 2
2.0
>>> 4 % 3
1

Looping

Looping walks through an iterable using for ... in or while:

>>> for item in ['a', 'b', 'c']:
...     print(item)

Linked from

Related notes