Variables & Types
2.0 Intro · 2.1 Variables · 2.2 Operators · 2.3 Types · 2.4–2.6 Built-ins & Collections
← → or Space to navigate · F for fullscreen
Assignment, naming conventions, and dynamic typing
=
x = 42 # int name = "Alice" # str pi = 3.14159 # float active = True # bool # Multiple assignment a, b, c = 1, 2, 3 # Swap without a temp variable a, b = b, a # Augmented assignment x += 10 # same as x = x + 10
Arithmetic, comparison, logical, and assignment operators
+
-
*
/
5 / 2
2.5
//
5 // 2
2
%
5 % 2
1
**
2 ** 8
256
==, !=, <, >, <=, >= → returns True/False
==
!=
<
>
<=
>=
True
False
x = 5 print(x > 0 and x < 10) # True print(x < 0 or x > 3) # True print(not x == 5) # False
and stops at the first False. or stops at the first True.
and
or
data = None result = data or [] # [] if data is falsy
Numbers, strings, booleans, and type conversion
int
42
-7
float
3.14
1e-6
complex
2+3j
j
print(type(42)) # <class 'int'> print(type(3.14)) # <class 'float'> print(10 / 3) # 3.3333... print(10 // 3) # 3
int("42") # 42 float("3.14") # 3.14 str(100) # '100' bool(0) # False bool("hello") # True
0, "", [], {}, None are all falsy. Everything else is truthy.
0
""
[]
{}
None
s[0]
s[1:4]
.upper()
.split()
.strip()
.replace()
s = "Hello, World!" print(s[0]) # H print(s[-1]) # ! print(s[7:12]) # World print(s.lower()) # hello, world! print(s.split(",")) # ['Hello', ' World!'] print(len(s)) # 13
list
tuple
dict
set
nums = [1, 2, 3] # list point = (3.0, 4.0) # tuple person = {"name": "Alice"} # dict unique = {1, 2, 3} # set
x = value
x += 1
x *= 2
2 ** 10
type(x)
isinstance(x, int)
int()
float()
str()
bool()
Next: Chapter 3 — Control Flow
if · elif · else · for · while · break · continue