Professional Documents
Culture Documents
Chapter 1
Chapter 1
Python is a high-level, interpreted programming language that is widely used for web development,
scientific computing, data analysis, artificial intelligence, and many other applications. In this
chapter, we will cover the basics of Python and how to get started with writing and running Python
programs.
To get started with Python, you first need to download and install it on your computer. Here are the
steps for installing Python on Windows:
Select the appropriate version of Python for your operating system (e.g., Windows, Mac, or Linux)
Once Python is installed, you can open a command prompt or terminal window and type "python" to
start the Python interpreter.
print("Hello, World!")
To run this program, open a command prompt or terminal window, navigate to the directory where
the program is saved, and type "python hello.py" (assuming the file is named hello.py). You should
see the message "Hello, World!" printed to the console.
Python has several built-in data types, including integers, floating-point numbers, strings, and
booleans. Let's create some variables and assign them different data types:
# Integer
age = 25
# Floating-point number
price = 4.99
# String
name = "Alice"
# Boolean
is_student = True
We can also perform mathematical and comparison operations using Python's operators:
# Mathematical operations
x = 10 + 5 # addition
y = 20 - 10 # subtraction
z = 5 * 6 # multiplication
w = 10 / 2 # division
# Comparison operations
a = 10 == 5 # equal to
b = 20 != 10 # not equal to
c = 5 > 2 # greater than
d = 10 < 20 # less than
Section 1.4: Control Flow
Python has several control flow statements that allow us to execute different blocks of code
depending on certain conditions. Here are some examples:
# if-else statement
# while loop
i=0
while i < 5:
print(i)
i += 1
# for loop
print(fruit)