Variables and Data Types
Python is a dynamically typed programming language, meaning you do not need to declare variable types before using them. Below are examples of variable declaration and some common data types:
Variable declaration:
Common data types:
- Integer (
int
):age = 25
- Floating-point number (
float
):pi = 3.14
- String (
str
):name = "John"
- Boolean (
bool
):is_true = True
Conditional Statements
Conditional statements in Python are used to check conditions and execute statements based on the evaluation result. The if
, else
, and elif
(else if) structures are used as follows:
if
statement:
else
statement:
elif
(else if
) statement:
Loops
Python supports two commonly used loop types: for
loop and while
loop, enabling repetitive execution of statements.
for
loop:
while
loop:
Specific Example:
When executed, the above code will check the age and print the appropriate message, then loop the Hello there!
message five times using a for
loop, and finally print the values of the while
loop.