DEV Community

Kemboijebby
Kemboijebby

Posted on

Python For beginners

Python for Beginners: A Friendly Introduction to the World’s Most Popular Programming Language

Have you ever thought about learning to code but didn’t know where to start? Python is one of the best programming languages for beginners. It’s simple, powerful, and used by millions of developers worldwide — from data scientists to web developers to automation experts.

What is Python?

Python is a high-level, general-purpose programming language created by Guido van Rossum and released in 1991. Its main goal is to make programming easy and fun. Python’s clear syntax and readability make it an excellent choice for first-time programmers.

Why Learn Python?

  • Beginner-friendly: The syntax is easy to read — it almost looks like plain English.
  • In-demand: Python is one of the most popular languages used by top companies like Google, NASA, and Netflix.
  • Versatile: You can use Python for web development, data analysis, AI and machine learning, automation, game development, and more.
  • Huge community: Tons of free tutorials, libraries, and active forums make it easy to find help.

How to Get Started

  1. Install Python: Download Python from python.org. Follow the installation steps for your operating system.
  2. Choose an IDE or Text Editor: Popular options include VS Code, PyCharm, or even IDLE (which comes with Python).
  3. Write Your First Program: Open your editor and type:

print("Hello, World!")
Enter fullscreen mode Exit fullscreen mode

Save the file as hello.py and run it. If you see Hello, World! on your screen — congratulations, you just wrote your first Python program!.

Basic Concepts
_ Variables and Data Types_

name = "Alice"       # A string (text)
age = 25             # An integer (whole number)
height = 5.6         # A float (decimal number)
is_student = True    # A boolean (True or False)
Enter fullscreen mode Exit fullscreen mode
  • name stores text — we use quotes to show it’s a string.
  • age is a whole number.
  • height is a decimal number, called a float.
  • is_student is a boolean value — it can only be True or False.

Data structures in python

After learning about simple variables, you’ll often need to store collections of data. Python has built-in data structures for this — here are the most common one.

1. Lists

A list stores multiple items in a single variable. Lists are ordered and changeable.

fruits = ["apple", "banana", "cherry"]
print(fruits[0])  # Outputs: apple
fruits.append("orange")  # Add a new item
print(fruits)
Enter fullscreen mode Exit fullscreen mode

2. Tuples

A tuple is like a list, but unchangeable (immutable).

coordinates = (10, 20)
print(coordinates[1])  # Outputs: 20
Enter fullscreen mode Exit fullscreen mode

3. Dictionaries

A dictionary stores data in key-value pairs, like a real-life dictionary.

person = {
    "name": "Alice",
    "age": 25,
    "is_student": True
}
print(person["name"])  # Outputs: Alice

Enter fullscreen mode Exit fullscreen mode

4. Sets

A set is an unordered collection of unique items.

colors = {"red", "green", "blue"}
colors.add("yellow")
print(colors)
Enter fullscreen mode Exit fullscreen mode

Loops in Python

Loops let you repeat actions in your code — so you don’t have to write the same instructions again and again. Python has two main types of loops:
1.for Loop

A for loop is used to iterate over a sequence (like a list, tuple, or string).

fruits = ["apple", "banana", "cherry"]

for fruit in fruits:
    print(fruit)
Enter fullscreen mode Exit fullscreen mode

👉 This prints each fruit in the list one by one.

You can also use range() to loop a certain number of times:

for i in range(5):
    print(i)
Enter fullscreen mode Exit fullscreen mode

👉 This prints numbers from 0 to 4.

2. while Loop

A while loop keeps running as long as a condition is True.

count = 0

while count < 5:
    print(count)
    count += 1  # Increase count by 1
Enter fullscreen mode Exit fullscreen mode

👉 This prints numbers from 0 to 4.

3. break and continue

break stops the loop completely.
continue skips the rest of the current loop iteration and moves to the next one.

Example:

for i in range(5):
    if i == 3:
        break   # Stops the loop when i is 3
    print(i)
Enter fullscreen mode Exit fullscreen mode

Functions in Python

Functions let you group code into reusable blocks. They help you write cleaner, more organized programs.
🔹 What is a Function?
A function is like a recipe: you give it ingredients (inputs) and it gives you a result (output).
🔹 How to Define a Function
You define a function using the def keyword.

Example:

def greet():
    print("Hello, welcome to Python!")

Call it by its name:

greet()  # Outputs: Hello, welcome to Python!
Enter fullscreen mode Exit fullscreen mode

🔹 Functions with Parameters
You can pass arguments to a function to make it more flexible.

def greet(name):
    print(f"Hello, {name}!")

greet("Alice")  # Outputs: Hello, Alice!
greet("Bob")    # Outputs: Hello, Bob!
Enter fullscreen mode Exit fullscreen mode

🔹 Functions with Return Values

Sometimes you want the function to send back a result.

def add(a, b):
    return a + b

result = add(5, 3)
print(result)  # Outputs: 8
Enter fullscreen mode Exit fullscreen mode

** Why Use Functions? **

  • Reuse code: Write once, use many times.
  • Organize code: Break big problems into smaller parts.
  • Make code readable: Easier to test, debug, and understand.

Introduction to OOP in Python

Object-Oriented Programming (OOP) is a way of structuring your code by using classes and objects.
It helps you model real-world things like cars, people, or bank accounts as objects with properties (attributes) and behaviors (methods).

🔹 What is a Class?
A class is like a blueprint for creating objects.
Example:

class Dog:
    def __init__(self, name, age):
        self.name = name      # Attribute
        self.age = age

    def bark(self):
        print(f"{self.name} says woof!")
Enter fullscreen mode Exit fullscreen mode

🔹 What is an Object?

An object is an instance of a class — like a real dog made from the Dog blueprint.
Example:


my_dog = Dog("Buddy", 3)
print(my_dog.name)  # Outputs: Buddy
my_dog.bark()       # Outputs: Buddy says woof!
Enter fullscreen mode Exit fullscreen mode

Key OOP Concepts
Class:The blueprint for creating objects
Object:A specific instance of a class
Attribute:Variables that belong to the object
Methods:Functions that belong to the object
init Method:A special method called when you create an object (initializer or constructor)

Why Use OOP?

  • Makes your code organized and reusable
  • Models real-world things naturally
  • Helps you build larger programs step by step

A Simple Example
Let’s say you want to model a car:

class Car:
    def __init__(self, make, model):
        self.make = make
        self.model = model

    def drive(self):
        print(f"The {self.make} {self.model} is driving!")

my_car = Car("Toyota", "Corolla")
my_car.drive()  # Outputs: The Toyota Corolla is driving!
Enter fullscreen mode Exit fullscreen mode

Top comments (0)