Introduction
Python is a general-purpose programming language known for its simplicity and readability. It was created by Guido van Rossum and released in 1991.
Python’s design philosophy emphasizes code that reads like plain English, which is why it's popular among beginners and experts alike. It supports multiple programming paradigms such as procedural, object-oriented, and functional programming.
Whether you're learning Python for data science, automation, or web development, this guide provides a strong foundation to get you started:
- Understand what Python is and why it's widely used across industries.
- Explore the syntax and semantics of the Python programming language.
- Learn through concise explanations and hands-on code examples.
Prerequisites
Before diving into this guide, it’s helpful to have a bit of prior knowledge. While we aim to keep things simple, some concepts may assume you're familiar with basic programming terminology.
- A general understanding of how code is written and executed (e.g., variables, functions).
- Familiarity with any programming language (even HTML or JavaScript) is beneficial but not required.
Hello World
A "Hello, World!" program is typically used as a first step into any new language. In Python, printing text to the screen is done with the print() function. This program demonstrates how Python syntax is minimal and human-readable.
It serves two main purposes:
- Ensures your Python environment is set up correctly
- Teaches you how output works in Python
Print a message onto the screen:
print("Hello World")
Variables and Data Types
Python does not require you to declare a variable’s type before using it. When you assign a value to a variable, Python figures out the type automatically. This makes development faster, but it also means you need to be careful with dynamic typing.
Below are examples of how to create and use each of these data types:
# Integer
age = 25
print(age)
# Float
pi = 3.14159
print(pi)
# String
greeting = "Hello, world!"
print(greeting)
# Boolean
is_active = True
print(is_active)
Conditional Statements
Python uses if, elif, and else to perform logic checks. Conditions help control the flow of a program based on truth values.
Examples of conditional statements:
number = int(input('Enter a number: '))
# check if number is greater than 0
if number > 0:
print(f'{number} is a positive number.')
print('A statement outside the if statement.')
Loops
Python has two main types of loops:
- for loop: used to iterate over a sequence like a list or range of numbers
- while loop: continues as long as a condition is true
Below are examples of how loop:
for val in sequence:
# run this code
The for loop iterates over the elements of sequence in order, and in each iteration, the body of the loop is executed.
The loop ends after the body of the loop is executed for the last item.
In Python, we use a while loop to repeat a block of code until a certain condition is met. For example:
while condition:
# body of while loop
Functions
Functions are reusable blocks of code designed to perform a specific task. They're defined using the def keyword.
You can pass data, known as parameters, into a function.
In Python a function is defined using the def keyword:
def my_function():
print("Hello from a function")
To call a function, use the function name followed by parenthesis:
def my_function():
print("Hello from a function")
my_function()
Error Handling
Python provides structured error handling using try, except, else, and finally blocks.
This allows you to catch exceptions (unexpected errors) and respond gracefully, rather than crashing the entire program. Example:
#The try block will generate an exception, because x is not defined
try:
print(x)
except:
print("An exception occurred")