Starting Simple: Why Variables Matter
In Python, variables are containers with labels that hold whatever data you want—text, numbers, lists, you name it. You don’t need to tell Python what type of data is in there; it just goes with the flow.
Example:
name = "Alice"
age = 25
height = 5.5
Here, name
is a string, age
is an integer, and height
is a float. Now you can store info without getting bogged down in type declarations.
Diving into Data Types
Python data types cover pretty much all your needs:
-
Integer (
age = 25
): Whole numbers. -
Float (
height = 5.5
): Numbers with decimals. -
String (
name = "Alice"
): A sequence of characters. -
Boolean (
is_active = True
): True or False. - Lists, Tuples, Sets, and Dictionaries: Collections of items—like keeping a bunch of things in separate, labeled boxes.
Operators: Simple Calculations, Big Impact
You’ve got your arithmetic operators (+
, -
, *
, /
), but Python’s also got some extras:
Examples:
a = 10
b = 3
print(a + b) # Addition
print(a ** b) # Exponentiation (fancy word for power)
print(a // b) # Floor division (cuts off decimals)
Real-World Example: Checking Access
Here’s a quick profile check: if the age falls between 18 and 60 and the user’s an admin, they get access.
age = 30
is_admin = True
if 18 <= age <= 60 and is_admin:
print("Access Granted")
else:
print("Access Denied")
There you go!
As a wise coder once said, "May your bugs be few and your syntax be clean."
Top comments (1)
Informative :)