πΆ Python Basics#
Welcome to Python Basics! Mastering this chapter will give you the foundation for everything else in programming. Letβs start with the very simple concepts and grow step by step. π±
What Youβll Learn π―#
- Python syntax and indentation
- Printing messages
- Basic data types: integers, floats, strings, booleans
- Variables: storing data
- Comments
1. Python Syntax & Indentation π¨#
- Python cares about indentation! Indentation (spaces at the start of a line) decides structure in Python.
- Every new code block (like after :) uses the same indentation.
if True:
print("Hello!") # This line is indented
2. Printing Messages π’#
Use the print()
function to display output.
print("Welcome to Python! π")
Output:
Welcome to Python! π
3. Variables: Storing Data π¦#
A variable is a name for a value. Assignment uses =
.
name = "Alex"
age = 21
height = 1.75
is_student = True
name
holds a string ("Alex"
)age
is an integer (21
)height
is a float (1.75
)is_student
is a boolean (True
)
4. Data Types Explained π€#
Data Type | Example | Description |
---|---|---|
int | 5 | Whole numbers |
float | 3.14 | Decimal numbers |
str | "Hello" | Text |
bool | True | True/False values |
You can check a variableβs type with type()
:
print(type(name)) # <class 'str'>
print(type(age)) # <class 'int'>
print(type(height)) # <class 'float'>
print(type(is_student)) # <class 'bool'>
5. Comments π#
Use the hash sign (#
) to add comments (notes the program ignores).
# This is a comment!
print("Python is fun!") # This prints a message
Practice Questions with Solutions π#
- Practice: Create a variable called
city
that stores your city name and print it.- Solution:
city = "Delhi"
print(city)
- Practice: What is the output?
a = 10
b = 3.5
print(a + b)
- **Solution:**
13.5
3. Practice: Use comments to describe the line.
- Solution:
# Assigning a value to temperature
temperature = 37.5
Step-by-Step Mini Project: Printing a Bio Card π«#
Letβs use what we learned!
# My Bio Card
name = "Sara"
age = 19
is_new_student = True
print("Name:", name)
print("Age:", age)
print("New Student?", is_new_student)
Output:
Name: Sara
Age: 19
New Student? True
Checklist for This Chapter β #
- Understood Python indentation
- Used
print()
to display output - Created and used variables of different types
- Used comments to clarify code
Youβve just built a strong Python foundation! All set for more awesome skills ahead! ππ