Basics of Python
Section 1: Basics of Python
Lesson 1: Python Syntax and Structure
1.1 Understanding Indentation and Code Blocks
In Python, indentation is used to define code blocks. Proper indentation (4 spaces) is crucial for the interpreter to understand the structure of the code.
Example:
# Correct indentation
if True:
print("This is indented correctly")
# Incorrect indentation (will result in an error)
if True:
print("This will cause an error")
1.2 Basic Structure of a Python Script
A Python script typically starts with the shebang line (on Unix-based systems) and includes statements and functions.
Example:
#!/usr/bin/env python3
# This is a simple Python script
print("Hello, World!")
Lesson 2: Variables and Data Types
2.1 Creating and Assigning Variables
Variables are used to store data in Python. You can create and assign values to variables.
Example:
# Creating and assigning variables
name = "John"
age = 25
# Printing variables
print("Name:", name)
print("Age:", age)
2.2 Different Data Types in Python
Python supports various data types, including integers (int), floating-point numbers (float), and strings (str).
Example:
# Different data types
integer_variable = 42
float_variable = 3.14
string_variable = "Hello, Python!"
# Displaying data types
print("Integer Variable:", type(integer_variable))
print("Float Variable:", type(float_variable))
print("String Variable:", type(string_variable))
Lesson 3: Control Flow and Decision Making
3.1 Conditional Statements (if, elif, else)
Conditional statements allow you to make decisions in your code based on certain conditions.
Example:
# Conditional statements
x = 10
if x > 0:
print("Positive number")
elif x == 0:
print("Zero")
else:
print("Negative number")
3.2 Logical Operators
Logical operators (and, or, not) help combine multiple conditions in conditional statements.
Example:
# Logical operators
age = 25
if age >= 18 and age <= 65:
print("Eligible for work")
else:
print("Not eligible for work")
Lesson 4: Loops and Iteration
4.1 For Loops, While Loops
Loops allow you to repeat a block of code multiple times. for loops iterate over a sequence, and while loops continue until a condition is no longer true.
Example:
# For loop
for i in range(5):
print(i)
# While loop
counter = 0
while counter < 5:
print(counter)
counter += 1
4.2 Loop Control Statements (break, continue)
Loop control statements alter the flow of a loop. break terminates the loop, and continue skips the rest of the code in the loop and moves to the next iteration.
Example:
# Loop control statements
for i in range(10):
if i == 5:
break # Terminate the loop when i is 5
elif i % 2 == 0:
continue # Skip even numbers
print(i)
In this section, we covered the basics of Python programming, including syntax, structure, variables, data types, control flow, and loops. The provided code examples illustrate fundamental concepts, helping you grasp the essentials of Python programming. As you continue your journey in Python, these foundational skills will serve as building blocks for more advanced programming concepts and projects.