A beginner-friendly guide to learning Python programming from scratch.
Python is one of the most beginner-friendly and versatile programming languages, making it an excellent choice for those new to coding. Here’s a step-by-step guide to help you get started with Python:
python --version
python3 --version
hello.py
.print("Hello, World!")
python hello.py
Hello, World!
Start with the fundamentals of Python:
# Variables
name = "Alice"
age = 25
height = 5.6
is_student = True
# Data Types
print(type(name)) # <class 'str'>
print(type(age)) # <class 'int'>
print(type(height)) # <class 'float'>
print(type(is_student)) # <class 'bool'>
name = input("Enter your name: ")
print(f"Hello, {name}!")
age = 18
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
# For loop
for i in range(5):
print(i) # Prints 0 to 4
# While loop
count = 0
while count < 5:
print(count)
count += 1
def greet(name):
print(f"Hello, {name}!")
greet("Alice")
# List
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # apple
# Dictionary
person = {"name": "Alice", "age": 25}
print(person["name"]) # Alice
Apply what you’ve learned by building small projects:
Example: Guess the Number
import random
number = random.randint(1, 100)
guess = None
while guess != number:
guess = int(input("Guess a number between 1 and 100: "))
if guess < number:
print("Too low!")
elif guess > number:
print("Too high!")
else:
print("You got it!")
Python has a rich ecosystem of libraries for various tasks. Some popular ones include:
Install libraries using pip
:
pip install numpy pandas matplotlib
Python supports OOP, which helps you organize code into reusable objects.
Example:
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def bark(self):
print(f"{self.name} says woof!")
my_dog = Dog("Rex", 3)
my_dog.bark() # Rex says woof!
Learn how to read from and write to files.
Example:
# Writing to a file
with open("example.txt", "w") as file:
file.write("Hello, World!")
# Reading from a file
with open("example.txt", "r") as file:
content = file.read()
print(content) # Hello, World!
By following these steps and consistently practicing, you’ll quickly become proficient in Python and be ready to tackle more advanced topics and projects. Happy coding! 🚀