Getting Started with Python Programming
Learn the fundamentals of Python programming language with practical examples and explanations.
Getting Started with Python Programming
Python is a popular, versatile programming language known for its readability and ease of use. It’s a great language for beginners and is widely used in data science, web development, automation, and many other fields.
Installing Python
Before we can start coding in Python, we need to install it on our computer.
Windows Installation
- Visit the official Python website
- Download the latest version for Windows
- Run the installer and check “Add Python to PATH”
- Click “Install Now”
MacOS Installation
MacOS comes with Python pre-installed, but it’s usually an older version. To install the latest version:
- Install Homebrew if you don’t have it already:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Install Python using Homebrew:
brew install python
Linux Installation
Most Linux distributions come with Python pre-installed. You can check your version with:
python3 --version
If you need to install it, use your package manager:
# For Debian/Ubuntu
sudo apt update
sudo apt install python3 python3-pip
Your First Python Program
Let’s write our first Python program, the classic “Hello, World!”:
# This is a comment
print("Hello, World!")
Save this code in a file named hello.py
and run it from your terminal:
python hello.py
You should see the output: Hello, World!
Variables and Data Types
Python has several built-in data types:
# Integer
age = 25
# Float
height = 1.75
# String
name = "John Doe"
# Boolean
is_student = True
# List
fruits = ["apple", "banana", "cherry"]
# Dictionary
person = {
"name": "John",
"age": 25,
"city": "New York"
}
Control Flow
Conditional Statements
age = 18
if age < 18:
print("You are a minor")
elif age >= 18 and age < 65:
print("You are an adult")
else:
print("You are a senior")
Loops
Python provides two types of loops:
# For loop
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(f"I like {fruit}")
# While loop
count = 0
while count < 5:
print(f"Count is {count}")
count += 1
Functions
Functions are defined using the def
keyword:
def greet(name):
"""This function greets the person passed in as a parameter"""
return f"Hello, {name}!"
# Function call
message = greet("Alice")
print(message) # Output: Hello, Alice!
B[Define Constants]
B --> C[Define Functions]
C --> D[Define Classes]
D --> E[Main Program Logic]
E --> F[Program Execution]
style A fill:#f9f,stroke:#333,stroke-width:2px
style F fill:#bbf,stroke:#333,stroke-width:2px
—>
Object-Oriented Programming
Python is an object-oriented programming language. Here’s a simple class definition:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
return f"Hello, my name is {self.name} and I am {self.age} years old."
# Create an instance of Person
john = Person("John", 25)
print(john.greet())
WebDev[Web Development]
Python --> DataScience[Data Science]
Python --> Automation[Automation]
Python --> ML[Machine Learning]
Python --> GameDev[Game Development]
WebDev --> Flask
WebDev --> Django
WebDev --> FastAPI
DataScience --> Pandas
DataScience --> NumPy
DataScience --> Matplotlib
ML --> TensorFlow
ML --> PyTorch
ML --> Scikit-learn
style Python fill:#3776AB,stroke:#fff,color:#fff
“ —>
Exception Handling
Python uses try-except blocks for exception handling:
try:
# Code that might cause an exception
result = 10 / 0
except ZeroDivisionError:
# Handle the exception
print("Error: Division by zero")
finally:
# This code always executes
print("This is always executed")
Next Steps
Now that you’ve learned the basics of Python, you can:
- Practice with small projects
- Learn more about specific libraries based on your interests
- Join Python communities and forums
- Contribute to open-source projects
Conclusion
Python is a powerful and flexible language that can be used for a wide range of applications. Its clear syntax and extensive library ecosystem make it an excellent choice for both beginners and experienced programmers.
Happy coding!