On this page
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
+Addition-Subtraction*Multiplication**Exponentiation/True division (returnsfloat)//Floor division%Modulus (remainder)
>>> 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
- tuts/python3/tutorials/core_fundamentals Linked here; links to this page; shared tags: #fundamentals
- tuts/python3/tutorials/core_fundamentals/control_flow Linked here; links to this page; shared tags: #fundamentals
- tuts/python3/tutorials/core_fundamentals/syntax_and_variables Linked here; links to this page; shared tags: #fundamentals
- tuts/python3 Linked here; links to this page
- tuts/python3/python functions Linked here; links to this page
- tuts/python3/__builtins__ Linked here
- tuts/python3/__builtins__/id Linked here
- tuts/python3/__builtins__/isinstance Linked here
- tuts/python3/tutorials links to this page
- tuts/c/fundamentals shared tags: #fundamentals