, Chapter 1
Alright, let's start your Python journey! We'll cover the basics with clear explanations
and examples.
Lesson 1: Introduction to Python - Getting Started
1. What is Python?
Python is a versatile and easy-to-learn programming language.
It's used for various purposes, including web development, data analysis, and
automation.
Its clear syntax makes it beginner-friendly.
2. Setting up Python:
You'll need to install Python on your computer.
o Go to python.org and download the latest version.
o During installation, make sure to check the box that says "Add Python to PATH." This
will make it easier to run Python from your command line.
You'll also need a text editor or an Integrated Development Environment (IDE).
o IDEs like VS Code, PyCharm, or Thonny are great for beginners. They provide features
like code highlighting and debugging.
3. Your First Python Program: "Hello, World!"
Open your text editor or IDE.
Type the following code:
Python
print("Hello, World!")
Save the file as hello.py.
Open your command prompt or terminal.
Navigate to the directory where you saved the file.
, Run the program by typing: python hello.py
You should see "Hello, World!" printed on the screen.
Explanation:
print() is a built-in Python function that displays output on the console.
"Hello, World!" is a string, which is a sequence of characters enclosed in quotation
marks.
4. Variables:
Variables are used to store data.
You can assign values to variables using the = operator.
Python
name = "Alice"
age = 30
print(name)
print(age)
Explanation:
name and age are variables.
"Alice" is a string value assigned to name.
30 is an integer value assigned to age.
5. Data Types:
Python has several built-in data types:
o Integers (int): Whole numbers (e.g., 10, -5).
o Floats (float): Decimal numbers (e.g., 3.14, 2.5).
o Strings (str): Text (e.g., "Hello").
o Booleans (bool): True or False.
Python
number = 10
decimal = 3.14
text = "Python"
is_true = True
print(type(number)) #output <class 'int'>
print(type(decimal)) #output <class 'float'>
, print(type(text)) #output <class 'str'>
print(type(is_true)) #output <class 'bool'>
Explanation:
The type() function returns the data type of a variable.
6. Basic Operators:
Python supports various operators for performing calculations and comparisons:
o Arithmetic Operators: + (addition), - (subtraction), * (multiplication), / (division), // (floor
division), % (modulo).
o Comparison Operators: == (equal to), != (not equal to), > (greater than), < (less than),
>= (greater than or equal to), <= (less than or equal to). 1
Python
x = 10
y = 5
print(x + y) # Output: 15
print(x - y) # Output: 5
print(x * y) # Output: 50
print(x / y) # Output: 2.0
print(x == y) # Output: False
print(x > y) # Output: True
7. Input:
The input() function allows you to get input from the user.
Python
user_name = input("Enter your name: ")
print("Hello, " + user_name + "!")
Explanation:
The input() function displays a prompt and waits for the user to enter input.
The input is stored as a string in the user_name variable.
Practice Exercises:
1. Write a program that asks the user for their age and prints it.
2. Write a program that calculates the area of a rectangle (length * width).
3. Write a program that asks for two numbers and prints the sum, difference, product, and
quotient.
Alright, let's start your Python journey! We'll cover the basics with clear explanations
and examples.
Lesson 1: Introduction to Python - Getting Started
1. What is Python?
Python is a versatile and easy-to-learn programming language.
It's used for various purposes, including web development, data analysis, and
automation.
Its clear syntax makes it beginner-friendly.
2. Setting up Python:
You'll need to install Python on your computer.
o Go to python.org and download the latest version.
o During installation, make sure to check the box that says "Add Python to PATH." This
will make it easier to run Python from your command line.
You'll also need a text editor or an Integrated Development Environment (IDE).
o IDEs like VS Code, PyCharm, or Thonny are great for beginners. They provide features
like code highlighting and debugging.
3. Your First Python Program: "Hello, World!"
Open your text editor or IDE.
Type the following code:
Python
print("Hello, World!")
Save the file as hello.py.
Open your command prompt or terminal.
Navigate to the directory where you saved the file.
, Run the program by typing: python hello.py
You should see "Hello, World!" printed on the screen.
Explanation:
print() is a built-in Python function that displays output on the console.
"Hello, World!" is a string, which is a sequence of characters enclosed in quotation
marks.
4. Variables:
Variables are used to store data.
You can assign values to variables using the = operator.
Python
name = "Alice"
age = 30
print(name)
print(age)
Explanation:
name and age are variables.
"Alice" is a string value assigned to name.
30 is an integer value assigned to age.
5. Data Types:
Python has several built-in data types:
o Integers (int): Whole numbers (e.g., 10, -5).
o Floats (float): Decimal numbers (e.g., 3.14, 2.5).
o Strings (str): Text (e.g., "Hello").
o Booleans (bool): True or False.
Python
number = 10
decimal = 3.14
text = "Python"
is_true = True
print(type(number)) #output <class 'int'>
print(type(decimal)) #output <class 'float'>
, print(type(text)) #output <class 'str'>
print(type(is_true)) #output <class 'bool'>
Explanation:
The type() function returns the data type of a variable.
6. Basic Operators:
Python supports various operators for performing calculations and comparisons:
o Arithmetic Operators: + (addition), - (subtraction), * (multiplication), / (division), // (floor
division), % (modulo).
o Comparison Operators: == (equal to), != (not equal to), > (greater than), < (less than),
>= (greater than or equal to), <= (less than or equal to). 1
Python
x = 10
y = 5
print(x + y) # Output: 15
print(x - y) # Output: 5
print(x * y) # Output: 50
print(x / y) # Output: 2.0
print(x == y) # Output: False
print(x > y) # Output: True
7. Input:
The input() function allows you to get input from the user.
Python
user_name = input("Enter your name: ")
print("Hello, " + user_name + "!")
Explanation:
The input() function displays a prompt and waits for the user to enter input.
The input is stored as a string in the user_name variable.
Practice Exercises:
1. Write a program that asks the user for their age and prints it.
2. Write a program that calculates the area of a rectangle (length * width).
3. Write a program that asks for two numbers and prints the sum, difference, product, and
quotient.