The Complete Python Guide: Mastering Programming for Everyone

Abdul TalhaAbdul Talha
14 min read

Intro to Python

  • What is Python?

    Python is a popular programming language created by Guido van Rossum in 1989.

  • Why the Name "Python"?

    It’s named after the British comedy show Monty Python, not the snake!

  • Why is Python Popular?

    • Its syntax is simple and easy to learn, similar to the English language.

    • It’s beginner-friendly but also powerful enough for advanced tasks.

    • Python is versatile and widely used in web development, data analysis, artificial intelligence, and more.

How Python Works

Python is an interpreted language, which means it processes your code line by line instead of all at once. Let’s look at how it works behind the scenes:

1. Lexical Analysis

  • When you run a Python program, the first step is to break the code into small parts like keywords (if, for), variables (e.g., x, name), and symbols (+, =). These parts are called tokens.

  • Python also allocates memory for the variables you’ve used, ensuring everything runs smoothly.

2. Syntax Analysis

  • Syntax is just a fancy way of saying “rules for writing code.” For example, Python uses indentation (spaces at the start of a line) to organize blocks of code.

  • If you forget something like a colon (:) or use incorrect indentation, Python will throw a syntax error, and the program won’t run until you fix it.

3. Semantic Analysis

  • Once the syntax is correct, Python checks if the code makes sense. For example, if you try to add a number to a string (5 + "hello"), Python won’t understand it and will throw an error.

Variables in Python

  • What is a Variable?

    A variable is used to store a value in Python.

    • Example:

        a = 10  # a is variable, 10 is value
        b = 20  # b is variable, 20 is value
      
  • How to Get the Type of a Variable?

    We can check the data type of a variable using the type() function.

    • Example:

        a = 10
        print(type(a))  # Output: <class 'int'>
      

Data Types in Python

1. Numeric Types

  • Integer (int): Whole numbers like 10 or 5.

  • Float (float): Decimal numbers like 20.5 or 3.14.

  • Complex (complex): Numbers with a real and imaginary part, like 3 + 5j.

Example:

a = 10       # int
b = 20.5     # float
c = 3 + 5j   # complex

print(type(a))  # Output: <class 'int'>
print(type(b))  # Output: <class 'float'>
print(type(c))  # Output: <class 'complex'>

2. Boolean (bool)

  • Represents True or False. Example:
active = True
print(type(active))  # Output: <class 'bool'>

3. String (str)

  • A sequence of characters. Example:
name = "Python"
print(type(name))  # Output: <class 'str'>

Other Built-In Data Types

  • List: A collection of items, e.g., [1, 2, 3].

  • Tuple: An immutable collection, e.g., (1, 2, 3).

  • Set: A collection of unique items, e.g., {1, 2, 3}.

  • Dictionary: A collection of key-value pairs, e.g., {"key": "value"}.

Dynamic Input in Python

You can take user input dynamically and check its type.

  • Example:

      a = input("Enter a value: ")
      print(type(a))  # By default, input() returns a string
    
      b = int(input("Enter an integer: "))
      print(type(b))  # Converts input to an integer
    

Operators in Python

1. Arithmetic Operators

These operators perform basic mathematical operations:

  • Addition (+): Adds two numbers.

    Example:

      a = 5
      b = 3
      print(a + b)  # Output: 8
    
  • Subtraction (``): Subtracts one number from another.

      print(a - b)  # Output: 2
    
  • Multiplication (``): Multiplies two numbers.

      print(a * b)  # Output: 15
    
  • Division (/): Divides one number by another (returns a float).

      print(a / b)  # Output: 1.6667
    
  • Floor Division (//): Divides and truncates the result to the nearest integer.

      print(a // b)  # Output: 1
    
  • Modulus (%): Returns the remainder of the division.

      print(a % b)  # Output: 2
    
  • Exponentiation (``): Raises a number to the power of another.

      print(a ** b)  # Output: 125
    

2. Assignment Operators

These operators assign values to variables:

  • =: Assigns a value.

  • +=: Adds and assigns (x += 1 is the same as x = x + 1).

  • =: Subtracts and assigns.

  • =: Multiplies and assigns.

  • /=: Divides and assigns.

Example:

x = 10
x += 5
print(x)  # Output: 15

3. Comparison Operators

These compare two values:

  • ==: Equal to

  • !=: Not equal to

  • <: Less than

  • >: Greater than

  • <=: Less than or equal to

  • >=: Greater than or equal to

Example:

a = 10
b = 20
print(a == b)  # Output: False
print(a < b)   # Output: True

4. Logical Operators

These are used to combine conditional statements:

  • and: Returns True if both conditions are true.

  • or: Returns True if at least one condition is true.

  • not: Reverses the condition.

Example:

x = True
y = False
print(x and y)  # Output: False
print(x or y)   # Output: True
print(not x)    # Output: False

Type Conversion in Python

Type conversion means converting one data type to another.

Syntax:

datatype(variable)

Example:

a = "10"
b = int(a)  # Converts string to integer
print(type(b))  # Output: <class 'int'>

Exercises

  1. Convert String to Integer and Add with Float:

     a = "10"
     b = 20.5
     c = int(a) + b
     print(c)  # Output: 30.5
    
  2. Write a Program to Add Two Numbers:

     a = int(input("Enter first number: "))
     b = int(input("Enter second number: "))
     print("Sum:", a + b)
    
  3. Calculate the Perimeter and Area of a Rectangle:

     length = 7
     breadth = 5
     perimeter = 2 * (length + breadth)
     area = length * breadth
     print("Perimeter:", perimeter)  # Output: 24
     print("Area:", area)  # Output: 35
    
  4. Calculate Simple Interest:

     principal = 5000
     rate = 5
     time = 3
     simple_interest = (principal * rate * time) / 100
     print("Simple Interest:", simple_interest)  # Output: 750.0
    

Conditions

In programming, conditional statements are used to perform different actions based on different conditions.

if Statement Syntax

The if statement executes a block of code only if the specified condition evaluates to True.

# If Statement
if condition:
    # <stmt-1>
    # ...
    # <stmt-n>

Example:

num = 10
a = int(input("Enter a number: "))
if a == num:
    print("The number is equal to 10.")
if a == 20:
    print("The number is 20.")

if-else Statement Syntax

The if-else statement allows you to execute one block of code if a condition is True and another block if the condition is False.

if condition:
    # stmt-1 (executed if condition is True)
else:
    # stmt-2 (executed if condition is False)

Example:

# Check greater number
a, b = 20, 10
if a > b:
    print("a is greater")
else:
    print("b is greater")

if-elif-else Statement Syntax

The if-elif-else statement is used when there are multiple conditions to evaluate. It checks conditions sequentially, and the first True condition executes its block.

if cond1:
    # stmt-1
elif cond2:
    # stmt-2
elif cond-n:
    # stmt-n
else:
    # stmt-default

Example:

# Find the greatest of three numbers
a, b, c = 100, 80, 70
if a > b and a > c:
    print("a is the greatest")
elif b > a and b > c:
    print("b is the greatest")
else:
    print("c is the greatest")

Additional Example:

age = int(input("Enter your age: "))
if age < 18:
    print("You are a minor.")
elif age >= 18 and age < 60:
    print("You are an adult.")
else:
    print("You are a senior citizen.")

Checking if a Year is a Leap Year

A year is a leap year if:

  1. It is divisible by 4, and not divisible by 100, or

  2. It is divisible by 400.

Example 1:

year = 2024
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
    print("Leap year")
else:
    print("Not a leap year")

Calculate Simple Interest (SI) and Compound Interest (CI)

Simple Interest (SI):

The formula for calculating simple interest is:

SI = (P × T × R) / 100

Where:

  • P is the principal amount

  • T is the time in years

  • R is the rate of interest

Example:

P = int(input("Enter principal amount: "))
T = int(input("Enter time in years: "))
R = int(input("Enter rate of interest: "))
SI = (P * T * R) / 100
print("Simple Interest:", SI)

Compound Interest (CI):

The formula for calculating compound interest is:

CI = P × (1 + R/100)^T - P

Where:

  • P is the principal amount

  • T is the time in years

  • R is the rate of interest

Example:

P = int(input("Enter principal amount: "))
T = int(input("Enter time in years: "))
R = int(input("Enter rate of interest: "))
Amt = P * pow((1 + R / 100), T)
CI = Amt - P
print("Compound Interest:", CI)

Iterations and Loops

Loops allow the repetition of a block of code until a condition is no longer true. They are fundamental in programming for automating repetitive tasks.

Types of Loops in Python

  1. for Loop

    The for loop is used for iterating over a sequence (e.g., a list, tuple, string, or range).

    Syntax:

     for variable in sequence:
         # <stmt-1>
         # ...
         # <stmt-n>
    

    Example:

     for i in range(1, 11):  # Iterates from 1 to 10
         print(i)
    
  2. while Loop

    The while loop continues to execute a block of code as long as the condition is True.

    Syntax:

     while condition:
         # <stmt-1>
         # ...
         # <stmt-n>
    

    Example:

     count = 1
     while count <= 5:
         print(count)
         count += 1
    

Using for Loop with range()

The range() function generates a sequence of numbers and is often used with for loops.

Syntax:

for variable in range(start, end, step):
    # <stmt-1>
    # ...
    # <stmt-n>

Examples:

  1. Basic Range Iteration:

     for i in range(1, 11):  # Iterates from 1 to 10
         print(i)
    
  2. Step Value in Range:

     for i in range(0, 20, 2):  # Iterates from 0 to 18 in steps of 2
         print(i)
    
  3. Reverse Range:

     for i in range(10, 0, -1):  # Iterates from 10 to 1
         print(i)
    

Using for Loop with Data Structures

You can iterate over strings, lists, dictionaries, and other data structures using for loops.

Examples:

  1. Iterating Over a String:

     name = "xyzabc"
     for char in name:
         print(char, end=" ")
     # Output: x y z a b c
    
  2. Iterating Over a List:

     numbers = [1, 2, 3, 4, 5]
     for num in numbers:
         print(num)
    
  3. Iterating Over a Dictionary:

     data = {'name': 'Alice', 'age': 25}
     for key, value in data.items():
         print(f"{key}: {value}")
    

Palindrome Checker

A palindrome is a number or string that reads the same backward as forward.

Checking Palindrome for Numbers:

n = int(input("Enter a number: "))
temp = n  # Store the original number
rev = 0

# Reverse the number
while n > 0:
    rem = n % 10
    rev = (rev * 10) + rem
    n = n // 10

# Check if the number is a palindrome
if temp == rev:
    print("Palindrome")
else:
    print("Not a palindrome")

Checking Palindrome for Strings:

string = input("Enter a string: ")
if string == string[::-1]:  # Check if the string equals its reverse
    print("Palindrome")
else:
    print("Not a palindrome")

Nested Loops

A nested loop is a loop inside another loop. The outer loop runs its iterations, and for each iteration of the outer loop, the inner loop executes completely.

Syntax of Nested Loops

for var1 in range(start1, end1):  # Outer loop
    # Outer loop statements
    for var2 in range(start2, end2):  # Inner loop
        # Inner loop statements

Example:

for i in range(1, 4):  # Outer loop (iterates 3 times)
    print("Outer loop iteration:", i)
    for j in range(1, 3):  # Inner loop (iterates 2 times)
        print("  Inner loop iteration:", j)

Output:

Outer loop iteration: 1
  Inner loop iteration: 1
  Inner loop iteration: 2
Outer loop iteration: 2
  Inner loop iteration: 1
  Inner loop iteration: 2
Outer loop iteration: 3
  Inner loop iteration: 1
  Inner loop iteration: 2

Pattern Printing Using Nested Loops

Nested loops are commonly used for printing patterns. Let's explore a few examples:

Example 1: Print Right-Aligned Triangle

*
**
***
****
*****

Code:

n = 5  # Number of rows
for i in range(1, n + 1):  # Outer loop for rows
    for j in range(i):  # Inner loop for columns
        print("*", end="")
    print()  # Move to the next line after each row

Example 2: Print Alphabets in a Triangle

A
A B
A B C
A B C D
A B C D E

Code:

n = 5  # Number of rows
for i in range(1, n + 1):  # Outer loop for rows
    for j in range(65, 65 + i):  # ASCII values for letters
        print(chr(j), end=" ")  # Convert ASCII to character
    print()  # Move to the next line after each row

Example 3: Print Inverted Alphabet Triangle

ABCDE
ABCD
ABC
AB
A

Code:

n = 5  # Number of rows
for i in range(n, 0, -1):  # Outer loop for rows (decreasing)
    for j in range(65, 65 + i):  # ASCII values for letters
        print(chr(j), end=" ")  # Convert ASCII to character
    print()  # Move to the next line after each row

Example 4: Center-Aligned Triangle

    A
   A B
  A B C
 A B C D
A B C D E

Code:

n = 5  # Number of rows
for i in range(1, n + 1):  # Outer loop for rows
    print(" " * (n - i), end="")  # Print spaces for alignment
    for j in range(65, 65 + i):  # Inner loop for alphabets
        print(chr(j), end=" ")
    print()  # Move to the next line

Functions

A function is a block of code designed to perform a specific task. Functions make code more reusable, and they return a value when called.

Syntax of Functions

def function_name(arguments):
    # Statements
    return value

Example of a Simple Function

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

print(add(10, 20))  # Output: 30
print(add(20, 30))  # Output: 50

Purpose of Functions

The primary purpose of creating a function is reusability. You can call the function multiple times with different arguments, and it performs the task for you each time.

Example: Function that Squares a Number

def square(a):
    return a * a

print(square(4))  # Output: 16

Types of Functions

  1. With Arguments and With Return Type

  2. With Arguments and Without Return Type

  3. Without Arguments and With Return Type

  4. Without Arguments and Without Return Type

Example 1: With Arguments and With Return Type

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

print(add(10, 30))  # Output: 40

Example 2: Without Arguments and With Return Type

def name():
    return "Abdul"

print(name())  # Output: Abdul

Example 3: Function with Function

You can also call one function inside another:

def salary():
    return 4000

def name():
    return "Abdul"

x = name()
sal = salary()
print(f"{x} is getting a salary of {sal}")  # Output: Abdul is getting a salary of 4000

Lambda Functions

A lambda function is an anonymous function. It is defined using the lambda keyword and can take any number of arguments but only have one expression.

Syntax of Lambda Function

lambda arguments: expression

Example: Lambda Function to Square a Number

square = lambda num: num * num
print(square(4))  # Output: 16

Example: Lambda Function to Cube a Number

cube = lambda num: num ** 3
print(cube(3))  # Output: 27

Map Function

The map() function applies a given function to all items in a list (or any iterable).

Example: Using map() to Cube Numbers in a List

numbers = [1, 2, 3]
cubes = list(map(lambda x: x ** 3, numbers))
print(cubes)  # Output: [1, 8, 27]

Example: Multiply Each Element by 8 Using map()

numbers = [7, 1, 8, 6]
result = list(map(lambda x: x * 8, numbers))
print(result)  # Output: [56, 8, 64, 48]

Filter Function

The filter() function filters elements from a sequence based on a condition defined in a function. It returns the elements that satisfy the condition.

Syntax of filter() Function

filter(function, sequence)

Example: Filter Even Numbers

numbers = [1, 2, 3, 4, 5, 6]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers)  # Output: [2, 4, 6]

Example: Filter Numbers Divisible by 7

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
divisible_by_7 = list(filter(lambda x: x % 7 == 0, numbers))
print(divisible_by_7)  # Output: [7]

Challenges and Solutions

  1. Finding Errors:

    • Challenge: I didn’t understand error messages at first, so fixing mistakes in my code took longer.

    • Solution: I learned to read error messages carefully, which helped me fix issues faster.

  2. Understanding Logic:

    • Challenge: Writing conditions and loops was hard because I wasn’t always sure if my code would work as expected.

    • Solution: I learned that clear thinking is important, which helped me structure my code better.

  3. Debugging Skills:

    • Challenge: Debugging was tricky because I didn’t know how to find the problems in my code.

    • Solution: I practiced reading error messages and used online resources to understand and fix my mistakes.

  4. Logical Thinking:

    • Challenge: I sometimes struggled to organize my code logically, leading to unexpected results.

    • Solution: I broke problems into smaller steps and wrote pseudocode before coding, which helped me make sure everything worked correctly.

Resources Used

  • Online Tutorials: Websites like W3Schools offered clear explanations and hands-on exercises, making learning interactive and engaging.

  • YouTube Videos: I found tutorials from channels like Code with harry particularly helpful, as they provided visual explanations and real-world examples that reinforced my understanding.

  • Practice Sites: I leveraged platforms like HackerRank and LeetCode for coding challenges. These sites offered problems that helped me apply what I learned in real-world scenarios and sharpened my coding skills

0
Subscribe to my newsletter

Read articles from Abdul Talha directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Abdul Talha
Abdul Talha