Python Refresher: The Basics

A Rapid Overview of Fundamental Features and Syntax

Python Refresher: The Basics

Introduction

This is a refresher post about Python, intended to provide a quick review of its key features and syntax for both beginners and professionals seeking to revisit and reinforce their knowledge. It is not meant to be a comprehensive tutorial.

Python is a high-level, interpreted, dynamically-typed programming language that supports multiple programming paradigms, including procedural, object-oriented, and functional programming. Known for its simplicity and readability, Python is an excellent choice for a wide range of applications.

Basic Python Syntax

Variables and Data Types

Python supports various data types, including integers, floats, strings, and booleans. A variable can be assigned on the fly without explicit declaration to reserve memory space.

Here are some examples:

# integer
x = 10 

# float
y = 20.5 

# string
z = "Hello World"

# boolean
a = True
# Assigns 10 to x and 20 to y in a single line
x, y = 10, 20 

# Swaps the values of x and y
x, y = y, x

In Python, variables can be reassigned with different types, unlike in statically typed languages.

x = 5  # x is an integer
x = "Hello"  # Now x is a string

Indentation

Indentation is used to define blocks of code. The amount of indentation is flexible, but it must be consistent within a block:

a = 5
b = 3
if a > b:
    # This line is part of the if statement because it's indented
    print("a is greater than b!")  
else:
     print("b is greater than a!")

print("The End ...")

Comments

Comments are marked by the hash symbol (#) for single-line comments or with triple quotes (''' or """) for multi-line comments:

# This is a single-line comment

"""
This is a 
multi-line comment
"""

Data Structure

Lists

Lists are mutable and can store multiple items in a single variable. You can change their content.

# list
my_list = [1, 2, 3, "four", 5.0]

# Changes the second element
my_list[1] = "apple"

Tuples

Tuples are ordered and immutable. You can't change their content, but you can take portions of existing tuples to create new ones.

# tuple
my_tuple = (1, 2, 3, "four", 5.0)

t1 = (1, 2, 3)
t2 = t1 + (4, 5, 6)  # Creates a new tuple t2 which is (1, 2, 3, 4, 5, 6)

Dictionaries

Dictionaries are collections of key-value pairs that allow you to store complex information:

car_info = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1965,
  "colors": ["red", "white", "blue"],
  "image": "https://tinyurl.com/Ford-Mustang-1965"
}

Sets

Sets are unordered collections of unique elements:

# set
my_set = {1, 2, 3, 3, 3}  # Note that duplicate elements are automatically removed

Operators

Python supports various operators for performing mathematical, comparison, assignment, logical, and bitwise operations.

# mathematical operators
add = 1 + 2
subtract = 3 - 2
multiply = 2 * 3
divide = 6 / 2
modulus = 7 % 2
exponent = 2 ** 3

# comparison operators
is_equal = 1 == 1
is_not_equal = 1 != 2
is_greater_than = 2 > 1
is_less_than = 1 < 2
is_greater_than_or_equal_to = 2 >= 1
is_less_than_or_equal_to = 1 <= 2

# logical operators
and_operator = True and False
or_operator = True or False
not_operator = not True

Control Flow

Python supports conditional statements and loops for controlling the flow of execution.

Conditional Statements

a = 200
b = 33
if b > a:
    print("b is greater than a")
elif a == b:
    print("a and b are equal")
else:
    print("a is greater than b")

Loops

Python supports both for and while loops. The for loop can be used with the range function to repeat an action a certain number of times:

# for loop
for i in range(5):
    print(i)

# while loop
x = 0
while x < 5:
    print(x)
    x += 1

Functions

Python functions are defined using the def keyword.

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

print(greet("Ali"))

Python supports both positional and keyword arguments and also allows for default argument values.

def greet(name="World"):
    return f"Hello, {name}!"

print(greet())
print(greet("Ann"))

Classes and Objects

Python supports object-oriented programming with classes and objects.

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

    def showDescription(self):
        print("This car is a", self.brand, self.model)

my_car = Car("Ford","Mustang")
my_car.showDescription()

Resources:

  1. Python's official documentation: docs.python.org/3

  2. W3Schools Python tutorial (w3schools.com/python)