Back to Python
2026-04-265 min read

Notes (Python Programming)

Learn Notes (Python Programming) step by step with clear examples and exercises.

Why This Matters

Python is a versatile and widely-used programming language that plays an essential role in modern software development and data science. Its clean syntax, extensive library support, and readability make it an ideal choice for beginners and experienced developers alike. In this guide, we'll delve into the core concepts of Python programming, providing you with practical insights and real-world examples to help you master this powerful tool.

Prerequisites

To get the most out of this guide, it is recommended that you have a basic understanding of computer programming concepts such as variables, loops, functions, and data structures. Prior experience with another programming language may also be helpful but is not strictly necessary.

Core Concept

Python's simplicity lies in its clean syntax, which emphasizes readability over complexity. We'll explore the fundamental building blocks of Python, including variables, data types, functions, loops, conditional statements, and essential data structures like lists, tuples, dictionaries, and sets.

Variables

A variable is a named container for storing data in Python. To create a variable, simply assign a value to it using the equals sign (=). For example:

my_variable = 42
print(my_variable) # Outputs: 42

Data Types

Python has several built-in data types, including integers, floating-point numbers, strings, booleans, and lists. We'll examine each of these in detail, discussing their properties and how they can be manipulated using various Python functions.

Integers

Integers are whole numbers, such as 3 or -10. They can be used for counting and performing arithmetic operations.

integer_variable = 42
print(type(integer_variable)) # Outputs: <class 'int'>

Floating-Point Numbers (Floats)

Floating-point numbers, or floats, are real numbers that can have a fractional part. They can be used for calculations involving decimal values.

float_variable = 3.14159
print(type(float_variable)) # Outputs: <class 'float'>

Strings

Strings are sequences of characters, such as "Hello" or "Python". They can be used for storing and manipulating text data.

string_variable = "Hello, World!"
print(type(string_variable)) # Outputs: <class 'str'>

Booleans

Booleans are values that represent true or false. They can be used for conditional logic and decision-making in your code.

boolean_variable = True
print(type(boolean_variable)) # Outputs: <class 'bool'>

Lists

Lists are ordered, mutable collections of items. They can contain any type of data and are enclosed in square brackets ([]).

list_variable = [1, 2, 3, "apple", True]
print(type(list_variable)) # Outputs: <class 'list'>

Tuples

Tuples are ordered, immutable collections of items. They can contain any type of data and are enclosed in parentheses (()).

tuple_variable = (1, 2, 3, "apple", True)
print(type(tuple_variable)) # Outputs: <class 'tuple'>

Dictionaries

Dictionaries are unordered collections of key-value pairs. They can be used to store and retrieve data efficiently.

dictionary_variable = {"name": "Alice", "age": 30, "job": "software developer"}
print(type(dictionary_variable)) # Outputs: <class 'dict'>

Sets

Sets are unordered collections of unique items. They can be used for operations like intersection, union, and difference.

set_variable = {1, 2, 3, 2, 4}
print(type(set_variable)) # Outputs: <class 'set'>

Functions

Functions are reusable blocks of code that perform specific tasks. In Python, we define functions using the def keyword, followed by the function name and its parameters enclosed in parentheses. Here's an example:

def greet(name):
print("Hello, " + name + "!")

greet("Alice") # Outputs: Hello, Alice!

Loops

Loops allow you to iterate over a collection of items or perform repetitive tasks. Python provides two types of loops: for loops and while loops. We'll cover both in detail, demonstrating their usage with examples and best practices for efficient programming.

Conditional Statements

Conditional statements enable your code to make decisions based on certain conditions. In Python, we use if, elif, and else statements to create conditional logic. We'll explore various examples of conditional statements and discuss how they can be used to solve real-world problems.

Worked Example

To illustrate the concepts discussed so far, let's create a simple calculator that performs addition, subtraction, multiplication, and division operations.

def add(x, y):
return x + y

def subtract(x, y):
return x - y

def multiply(x, y):
return x * y

def divide(x, y):
if y == 0:
raise ValueError("Cannot divide by zero")
return x / y

print("Enter two numbers separated by a space, followed by the operation (add/subtract/multiply/divide).")
num1 = float(input())
operator = input()
num2 = float(input())

if operator == "add":
result = add(num1, num2)
elif operator == "subtract":
result = subtract(num1, num2)
elif operator == "multiply":
result = multiply(num1, num2)
elif operator == "divide":
result = divide(num1, num2)
else:
print("Invalid operation. Please use add, subtract, multiply, or divide.")

print("The result is:", result)

Common Mistakes

1. Forgetting Indentation

Python relies on indentation to denote blocks of code. If you forget to properly indent your code, Python will throw a syntax error.

2. Incorrectly Handling User Input

When working with user input, it's essential to ensure that the input is in the expected format and handle exceptions gracefully. Failing to do so can lead to unexpected behavior or crashes.

3. Misusing Variables

Variables should be named clearly and concisely, avoiding ambiguity. Additionally, variables should only be used for their intended purpose to avoid confusion and bugs.

Practice Questions

  1. Write a function that calculates the factorial of a given number using recursion.
  2. Create a Python script that reads a list of numbers from a file and returns the sum of all even numbers.
  3. Implement a simple password validator that checks for a minimum length, at least one uppercase letter, at least one lowercase letter, and at least one digit.

FAQ

Q: What is Python's default data type for variables?

A: In Python, variables do not have an explicit data type. Instead, Python automatically determines the data type based on the value assigned to the variable.

Q: How can I create a multi-line string in Python?

A: You can create a multi-line string by enclosing it between three quotes (""" or '''). For example:

multi_line_string = """
This is a
multi-line string.
"""
print(multi_line_string)

Q: What are some popular Python libraries for data analysis?

A: Some popular Python libraries for data analysis include NumPy, Pandas, Matplotlib, and Scikit-learn. Each of these libraries offers unique functionality to help you tackle various data analysis tasks efficiently.

Notes (Python Programming) | Python | XQA Learn