Python is a high-level, interpreted programming language known for its readability and simplicity. It was created by Guido van Rossum and first released in 1991. Python's design philosophy emphasizes code readability with its notable use of significant whitespace. This tutorial provides an introduction to Python, covering its key features, syntax, and best practices.
To start using Python, you need to install it on your system. Follow these steps:
Download Python:
Install Python:
Verify Installation:
python --version or python3 --version.Python syntax is straightforward, focusing on readability and simplicity. Here are some basic elements:
Python uses indentation to define code blocks instead of curly braces {} found in other languages like C or Java. A standard practice is to use 4 spaces per indentation level.
# Example of Python indentation
if True:
print("This will be printed")
Comments are used to explain the code and are ignored by the interpreter. Single-line comments start with #, while multi-line comments can be created using triple quotes ''' or """.
# This is a single-line comment
"""
This is a
multi-line comment
"""
Python is dynamically typed, meaning you don’t need to declare the type of a variable. Common data types include integers, floats, strings, lists, tuples, dictionaries, and sets.
# Integer
age = 25
# Float
height = 5.9
# String
name = "Alice"
# List
fruits = ["apple", "banana", "cherry"]
# Tuple
coordinates = (10, 20)
# Dictionary
person = {"name": "Bob", "age": 30}
# Set
unique_numbers = {1, 2, 3, 4}
Python supports various operators for arithmetic, comparison, logical, and assignment operations.
# Arithmetic operators
x = 5
y = 3
print(x + y) # Addition: 8
print(x - y) # Subtraction: 2
print(x * y) # Multiplication: 15
print(x / y) # Division: 1.666...
print(x % y) # Modulus: 2
print(x ** y) # Exponentiation: 125
# Comparison operators
print(x == y) # Equal to: False
print(x != y) # Not equal to: True
print(x > y) # Greater than: True
print(x < y) # Less than: False
print(x >= y) # Greater than or equal to: True
print(x <= y) # Less than or equal to: False
# Logical operators
a = True
b = False
print(a and b) # Logical AND: False
print(a or b) # Logical OR: True
print(not a) # Logical NOT: False
# Assignment operators
x += 10 # Equivalent to x = x + 10
y -= 5 # Equivalent to y = y - 5
Python supports various control structures like loops and conditional statements.
# If-else statement
temperature = 25
if temperature > 30:
print("It's hot")
elif temperature > 20:
print("It's warm")
else:
print("It's cold")
# Iterating over a list
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
# Using a while loop
count = 0
while count < 5:
print(count)
count += 1
Functions are blocks of code that perform specific tasks. They help in organizing and reusing code.
# Defining a function
def greet(name):
return f"Hello, {name}!"
# Calling the function
print(greet("Alice")) # Output: Hello, Alice!
Error handling is crucial for building robust applications. Python uses try, except, else, and finally blocks to handle exceptions.
# Basic error handling
try:
result = 10 / 0
except ZeroDivisionError as e:
print(f"An error occurred: {e}")
else:
print("No errors occurred")
finally:
print("Execution completed")
Python is a versatile and powerful language suitable for various applications. Its simplicity and readability make it an excellent choice for beginners, while its extensive libraries and community support cater to advanced developers as well. By understanding Python's basic syntax, control structures, functions, error handling, and best practices, you can start building efficient and maintainable applications.
In the next section of this course, we will dive deeper into Python's data structures and object-oriented programming concepts. Stay tuned!