Python Fundamentals Tutorial – Variables, Loops, Functions, OOP & APIs


Learn Python fundamentals with this complete tutorial. Covers variables, loops, functions, object-oriented programming (OOP), and working with APIs. Perfect for beginners, developers, and AI enthusiasts to build a solid Python foundation for programming, AI, data science, and web development.

1. Introduction to Python

Python is a powerful, easy-to-learn programming language widely used in web development, AI, data science, and automation. Its clear syntax and readability make it perfect for beginners.

Best Practices:

  1. Write clear, meaningful variable names.
  2. Follow PEP 8 style guide (Python standard).
  3. Use comments to explain your code (# This is a comment).

2. Variables in Python

Variables are containers to store data values. Python is dynamically typed, meaning you don’t need to declare the data type explicitly.

Example:


# Variables
name = "Chinmaya"
age = 25
height = 5.9

print(name, age, height)

Best Practices:

  1. Use snake_case for variable names: first_name, total_count.
  2. Avoid single-letter names unless used in loops.
  3. Keep variables meaningful and descriptive.

3. Loops in Python

Loops help automate repetitive tasks. Python has for and while loops.

For Loop Example:


for i in range(5):
print("Iteration", i)

While Loop Example:


count = 0
while count < 5:
print("Count:", count)
count += 1

Best Practices:

  1. Avoid infinite loops; ensure condition will eventually become False.
  2. Use break to exit loops early and continue to skip iterations.
  3. Prefer for loops over while loops when iterating a known range.

4. Functions in Python

Functions allow you to reuse code and improve readability.

Example:


def greet(name):
"""Returns a greeting message."""
return f"Hello, {name}!"

print(greet("Chinmaya"))

Best Practices:

  1. Give functions descriptive names (calculate_area instead of func1).
  2. Keep functions short and focused; one function should do one task.
  3. Use docstrings to describe what the function does.

5. Object-Oriented Programming (OOP)

OOP helps structure code with classes and objects for better organization and scalability.

Example:


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 object
person1 = Person("Chinmaya", 25)
print(person1.greet())

Key Concepts:

  1. Class: Blueprint for objects.
  2. Object: Instance of a class.
  3. Inheritance: Reuse and extend functionality.
  4. Encapsulation: Keep data safe using private variables.

Best Practices:

  1. Name classes in CamelCase (Person, Car).
  2. Keep methods focused and reusable.
  3. Avoid deeply nested code inside classes.

6. Working with APIs

APIs allow your Python program to communicate with other services. Python’s requests library makes it easy to send HTTP requests.

Example:


import requests

# Send GET request
response = requests.get("https://api.agify.io?name=Chinmaya")
data = response.json()

print("Response:", data)

Best Practices:

  1. Always handle errors with try/except.
  2. Use descriptive variable names (response, data).
  3. Respect API rate limits and authentication requirements.

Example with error handling:


try:
response = requests.get("https://api.agify.io?name=Chinmaya")
response.raise_for_status() # Raise error for bad response
data = response.json()
print(data)
except requests.exceptions.RequestException as e:
print("Error:", e)

7. Summary & Best Practices

By mastering these topics, beginners can build a solid foundation in Python:

  1. Variables: store and manipulate data effectively.
  2. Loops: automate repetitive tasks.
  3. Functions: write reusable, readable code.
  4. OOP: structure programs for scalability.
  5. APIs: interact with external data and services.

General Best Practices:

  1. Use comments and docstrings to explain your code.
  2. Follow PEP 8 style guide.
  3. Keep code modular and reusable.
  4. Test your code frequently and handle errors gracefully.