Welcome to your first step in learning Python, one of the most popular programming languages today. This tutorial will guide you through the process of installing Python, understanding basic syntax, and writing simple programs.
1. Installing Python
Step 1: Download Python
- Go to the official Python website.
- Navigate to the Downloads section and choose the version for your operating system (Windows, macOS, Linux).
- Download the installer.
Step 2: Install Python
- Run the installer. Ensure you check the box that says “Add Python to PATH” before clicking Install.
- Follow the on-screen instructions to complete the installation.
Step 3: Verify Installation
- Open your command prompt (Windows) or terminal (macOS/Linux).
- Type
python --version
and press Enter. You should see the version of Python that you installed.
python --version
2. Your First Python Program
Step 1: Open a Text Editor or IDE
You can write Python code in any text editor (Notepad, Sublime Text) or an Integrated Development Environment (IDE) like PyCharm or Visual Studio Code. For beginners, we recommend using Visual Studio Code.
Step 2: Write Your First Program
- Open your text editor or IDE.
- Create a new file and name it
hello_world.py
. - Type the following code:
print("Hello, World!")
Step 3: Run Your Program
- Save the file.
- Open your command prompt or terminal.
- Navigate to the directory where you saved
hello_world.py
. - Run the program by typing:
python hello_world.py
You should see the output: Hello, World!
3. Understanding Basic Syntax
Variables and Data Types
Variables store data that you can use later in your program. Python is dynamically typed, meaning you don’t need to declare the variable type explicitly.
# Integer
age = 25
# Float
price = 19.99
# String
name = "John Doe"
# Boolean
is_student = True
Basic Operations
Python supports various arithmetic operations.
# Addition
sum = 10 + 5
# Subtraction
difference = 10 - 5
# Multiplication
product = 10 * 5
# Division
quotient = 10 / 5
# Modulus
remainder = 10 % 3
Comments
Comments are lines of code that Python ignores. They are used to explain code.
# This is a single-line comment
"""
This is a
multi-line comment
"""
Control Structures
Conditional Statements
Conditional statements execute code based on whether a condition is true or false.
age = 20
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
Loops
Loops allow you to repeat code multiple times.
For Loop
for i in range(5):
print(i)
While Loop
count = 0
while count < 5:
print(count)
count += 1
Functions
Functions are reusable blocks of code that perform a specific task.
def greet(name):
print(f"Hello, {name}!")
greet("Alice")
4. Writing Simple Programs
Program 1: Simple Calculator
This program performs basic arithmetic operations.
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
if y == 0:
return "Cannot divide by zero"
return x / y
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
choice = input("Enter choice(1/2/3/4): ")
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
print(f"The result is: {add(num1, num2)}")
elif choice == '2':
print(f"The result is: {subtract(num1, num2)}")
elif choice == '3':
print(f"The result is: {multiply(num1, num2)}")
elif choice == '4':
print(f"The result is: {divide(num1, num2)}")
else:
print("Invalid input")
Program 2: Temperature Converter
This program converts temperature from Celsius to Fahrenheit.
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = celsius_to_fahrenheit(celsius)
print(f"{celsius} Celsius is equal to {fahrenheit} Fahrenheit")
5. Conclusion
Congratulations! You’ve written your first Python programs and learned the basics of Python programming. Keep practicing and exploring more advanced topics to become proficient in Python. Happy coding!