Python Basics
Variables
A variable is a name that stores a value. You create one by assigning with =. Python figures out the type for you.
Syntax:
name = "Ayesha"
age = 21
height = 5.4
is_student = True
print(name, age, height, is_student)Output
Ayesha 21 5.4 True
Naming rules
- Names can contain letters, digits and
_, but cannot start with a digit. - They are case-sensitive:
ageandAgeare different. - Use snake_case:
first_name,total_price. - Don't use keywords like
if,for,classor built-in names likelistandprint.
Multiple assignment
x, y, z = 1, 2, 3
a = b = 0
x, y = y, x # swap two values