A complete technical reference guide covering core Python syntax, variable manipulation, and scope fundamentals.
Python has several built-in data types used to define various operations and storage behaviors.
| Data Type | Description | Example Code | Output |
|---|---|---|---|
| int | Integer numbers | a = 25print(type(a)) |
<class 'int'>25 |
| float | Decimal numbers | b = 3.14print(type(b)) |
<class 'float'>3.14 |
| str | Text / String | s = "Hello"print(type(s)) |
<class 'str'>Hello |
| bool | True or False | flag = Falseprint(type(flag)) |
<class 'bool'>False |
| list | Ordered collection (mutable) | lst = [1, 2, 3]print(type(lst)) |
<class 'list'>[1, 2, 3] |
| tuple | Ordered collection (immutable) | tup = (1, 2, 3)print(type(tup)) |
<class 'tuple'>(1, 2, 3) |
| dict | Key-Value pairs | d = {"a": 1, "b": 2}print(type(d)) |
<class 'dict'>{'a': 1, 'b': 2} |
Python allows flexible assignment patterns for initializing and manipulating values across multiple variables simultaneously.
a = b = c = 100
print(a, b, c) # Output: 100 100 100
a, b, c = 10, 20, 30
print(a, b, c) # Output: 10 20 30
a, b = 5, 10
a, b = b, a
print(a, b) # Output: 10 5
Scope determines the visibility and lifecycle of variables within your code blocks.
x = "Global" # Global variable
def func():
y = "Local" # Local variable
print("Inside function, y =", y)
func()
# print(y) # Error! y is not accessible outside the function
print("Outside function, x =", x)
Inside function, y = Local
Outside function, x = Global
You can explicitly convert data from one type to another using built-in casting functions.
x = "100"
print(int(x) + 50) # Output: 150
y = 3.99
print(int(y)) # Output: 3
z = 10
print(str(z) + " is number") # Output: 10 is number
flag = "True"
print(bool(flag)) # Output: True
int() – converts to integerfloat() – converts to floatstr() – converts to stringbool() – converts to booleanlist() – converts to listtuple() – converts to tupledict() – converts to dictionaryPI = 3.14159 # Constant (by convention - use UPPERCASE)
MAX_USERS = 100
Note: Python does not enforce true immutable constants syntactically, but developers use UPPERCASE naming conventions to indicate values that should remain constant.
You can remove variables from memory using the del keyword.
a = 10
print(a) # Output: 10
del a # Delete variable reference
# print(a) # Error: name 'a' is not defined
You can verify whether a variable has been defined in the global scope using globals().
x = 50
if 'x' in globals():
print("x exists")
else:
print("x does not exist")
x exists
Bringing concepts together with basic variable declarations and formatted strings (f-strings):
name = "John"
age = 25
is_emp = True
salary = 55000.75
print(f"Name: {name}")
print(f"Age: {age}")
print(f"Employed: {is_emp}")
print(f"Salary: {salary}")
Name: John
Age: 25
Employed: True
Salary: 55000.75