Python Basics
1. Variables and Data Types
In Python, variables are used to store data that can be referenced and manipulated in your code. Python supports various data types, including integers, floats, strings, and booleans.
Example:
age = 25 # Integer height = 5.9 # Float name = "Alice" # String is_student = True # Boolean
Think of variables as containers that hold different types of information. For instance, an integer can store whole numbers, while a string can store text.
2. Control Structures: If Statements
Control structures allow you to control the flow of your program. One common control structure is the if statement, which executes a block of code only if a certain condition is true.
Example:
age = 18 if age >= 18: print("You are an adult.") else: print("You are a minor.")
Imagine you are at a movie theater. The if statement is like the ticket checker who lets you in only if you meet the age requirement. If you are 18 or older, you get a message saying "You are an adult." Otherwise, you get a message saying "You are a minor."