Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Python Tutorial

Introduction to Python

Python is a high-level, interpreted programming language known for its readability and versatility. It is widely used for web development, data analysis, artificial intelligence, scientific computing, and more. Python's syntax is designed to be easy to understand, making it an excellent choice for beginners.

Setting Up Python

To start coding in Python, you must first install it on your machine. You can download the latest version of Python from the official website: python.org.

Once installed, you can verify the installation by opening a terminal (Command Prompt on Windows, Terminal on macOS/Linux) and typing:

python --version
Python 3.x.x

Writing Your First Python Program

Let's write a simple program that prints "Hello, World!" to the console. Open your favorite code editor (such as VS Code) and create a new file named hello.py. Add the following code:

print("Hello, World!")

To run your program, navigate to the directory where the file is located and type:

python hello.py
Hello, World!

Variables and Data Types

In Python, you can store data in variables. Python supports various data types including integers, floats, strings, and booleans. Here are some examples:

# Variable assignment age = 25 # Integer height = 5.9 # Float name = "Alice" # String is_student = True # Boolean

To print these variables, you can use:

print(name, "is", age, "years old.")
Alice is 25 years old.

Control Structures

Control structures allow you to control the flow of your program. The most common structures are if statements and loops. Here's an example of an if statement:

if age >= 18: print(name, "is an adult.") else: print(name, "is a minor.")
Alice is an adult.

And here’s a simple loop using for:

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

Functions

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

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

Conclusion

This tutorial covered the basics of Python, including installation, writing your first program, variables, control structures, and functions. Python is a powerful language that can be used in various domains. As you continue your learning journey, consider exploring libraries such as NumPy, Pandas, and Flask for data science and web development.