Python Basics
Variables & Data Types
Dynamic typing, the core types (int, float, str, bool, None), type() and conversions, multiple assignment, and how Python variables are really references.
A variable is created the moment you assign to it. There is no declaration and no type keyword — the value carries the type.
age = 19 # int
cgpa = 8.75 # float
name = "Asha" # str
is_enrolled = True # bool
nothing = None # NoneType: "no value"
print(type(age), type(cgpa), type(name), type(is_enrolled), type(nothing))
age = "nineteen" # the same name can later hold a different type
print(type(age))<class 'int'> <class 'float'> <class 'str'> <class 'bool'> <class 'NoneType'> <class 'str'>
Core types
| Type | Example | Notes |
|---|---|---|
int |
42, -7, 10**30 |
unlimited size — no overflow |
float |
3.14, 2e-3 |
64-bit double precision |
complex |
3 + 4j |
built-in complex numbers |
str |
'hi', "hi", """multi-line""" |
immutable text |
bool |
True, False |
a subclass of int (True == 1) |
NoneType |
None |
the absence of a value |
Python integers never overflow — try a number that would break C's long long:
print(2 ** 100)
print(len(str(2 ** 1000)), "digits in 2^1000")
factorial = 1
for i in range(1, 26):
factorial *= i
print("25! =", factorial)1267650600228229401496703205376 302 digits in 2^1000 25! = 15511210043330985984000000
Converting between types
print(int("45") + 5) # str -> int
print(float("2.5") * 2) # str -> float
print(int(9.99)) # float -> int truncates towards zero
print(round(9.99), round(2.5), round(3.5)) # rounds to the nearest even on .5
print(str(100) + " marks") # int -> str
print(bool(0), bool(""), bool("0"), bool([]), bool(3))50 5.0 9 10 2 4 100 marks False False True False True
Naming rules and conventions
- Letters, digits and underscores; cannot start with a digit; case-sensitive.
- Cannot be a keyword:
if,for,class,def,True,None, … - Convention (PEP 8):
snake_casefor variables and functions,PascalCasefor classes,UPPER_CASEfor constants.
Multiple assignment and swapping
a, b, c = 1, 2, 3
x = y = 0
a, b = b, a # swap without a temporary variable
print(a, b, c, x, y)
first, *rest = [10, 20, 30, 40] # unpacking with a star
print(first, rest)2 1 3 0 0 10 [20, 30, 40]
Variables are references
In C, a variable is a box that holds a value. In Python, a variable is a name that refers to an object. Assignment binds a name to an object; it does not copy the object.
a = [1, 2, 3]
b = a # b refers to the SAME list
b.append(4)
print(a) # a sees the change
print(a is b, id(a) == id(b))
c = list(a) # make a real copy
c.append(5)
print(a, c, a is c)[1, 2, 3, 4] True True [1, 2, 3, 4] [1, 2, 3, 4, 5] False
This matters for mutable objects (lists, dicts, sets). Immutable ones (int, float, str, tuple) can never change in place, so sharing them is always safe.
Test yourself
4 questions · pick an answer to check it instantly
- 1
What is
type(3 / 2)?/always produces a float in Python 3. Use//for integer division. - 2
What does
int("3.7")do?int()of a string needs an integer literal. Useint(float("3.7"))to get 3. - 3
Which value is truthy?
Any non-empty string is truthy, even"False". - 4
After
a = [1]; b = a; b.append(2), what isa?aandbrefer to the same list object.