Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Comprehensive Python Tutorial

1. Introduction to Python

Python is a high-level, interpreted programming language known for its readability and versatility. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming. Python is widely used in web development, data science, automation, and more.

2. Setting Up Python

To start programming in Python, you need to install Python on your machine. You can download Python from the official website: python.org/downloads.

After downloading, follow the instructions to install Python. Make sure to add Python to your PATH during the installation process.

3. Writing Your First Python Program

Create a new file named hello.py and open it in your text editor. Type the following code:

print("Hello, World!")

Save the file and run it from the command line:

python hello.py
Hello, World!

4. Basic Syntax and Data Types

4.1 Variables and Data Types

Python supports various data types including integers, floats, strings, lists, tuples, and dictionaries. Here are some examples:

x = 10  # Integer
y = 3.14  # Float
name = "Alice"  # String
numbers = [1, 2, 3, 4, 5]  # List
person = {"name": "Alice", "age": 25}  # Dictionary

4.2 Basic Operations

Python supports basic arithmetic operations such as addition, subtraction, multiplication, and division:

a = 5
b = 2
print(a + b)  # Addition
print(a - b)  # Subtraction
print(a * b)  # Multiplication
print(a / b)  # Division
7
3
10
2.5

5. Control Flow

5.1 Conditional Statements

Python uses if, elif, and else statements for decision-making:

x = 10
if x > 5:
    print("x is greater than 5")
elif x == 5:
    print("x is 5")
else:
    print("x is less than 5")
x is greater than 5

5.2 Loops

Python supports for and while loops:

For Loop:

for i in range(5):
    print(i)
0
1
2
3
4

While Loop:

i = 0
while i < 5:
    print(i)
    i += 1
0
1
2
3
4

6. Functions

Functions are reusable blocks of code that perform a specific task. You can define a function using the def keyword:

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

print(greet("Alice"))
Hello, Alice

7. Modules and Packages

Modules are files containing Python code that can be imported into other scripts. Packages are collections of modules. You can use the import statement to use modules:

import math

print(math.sqrt(16))
4.0

8. File Handling

Python provides built-in functions to read and write files. Here is an example of reading and writing a file:

Writing to a file:

with open("example.txt", "w") as file:
    file.write("Hello, World!")

Reading from a file:

with open("example.txt", "r") as file:
    content = file.read()
    print(content)
Hello, World!

9. Error Handling

Python uses try, except, and finally blocks for error handling:

try:
    x = 1 / 0
except ZeroDivisionError:
    print("Cannot divide by zero")
finally:
    print("This will always execute")
Cannot divide by zero
This will always execute

10. Object-Oriented Programming

Python supports object-oriented programming (OOP). You can define classes and create objects:

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def greet(self):
        return f"Hello, my name is {self.name} and I am {self.age} years old"

person = Person("Alice", 25)
print(person.greet())
Hello, my name is Alice and I am 25 years old

11. Libraries and Frameworks

Python has a rich ecosystem of libraries and frameworks that extend its capabilities. Some popular libraries include:

  • NumPy: For numerical computations.
  • Pandas: For data manipulation and analysis.
  • Requests: For making HTTP requests.
  • Flask: For web development.
  • TensorFlow: For machine learning.

12. Conclusion

This tutorial has covered the basics of Python programming. Python is a versatile language with a vast ecosystem, making it suitable for a wide range of applications. Practice and explore further to become proficient in Python.