Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Introduction to Python

1. What is Python?

Python is a high-level, interpreted programming language known for its readability and simplicity. It is widely used in various fields, including web development, data analysis, artificial intelligence, scientific computing, and more.

2. Installing Python

You can download and install Python from the official website: python.org. Follow the instructions on the website to install Python on your operating system.

To check if Python is installed correctly, open your terminal or command prompt and type:

python --version

You should see output similar to this:

Python 3.9.1

3. Your First Python Program

Let's create a simple Python program that prints "Hello, World!" to the console. Open your favorite text editor, create a new file named hello.py, and add the following code:

print("Hello, World!")
                

Save the file and run it from the terminal or command prompt with the following command:

python hello.py

You should see the following output:

Hello, World!

4. Python Syntax and Basic Constructs

4.1 Variables

Variables are used to store data. In Python, you don't need to declare the type of a variable. Here's an example:

x = 5
y = "Hello"
print(x)
print(y)
                

This will output:

5
Hello

4.2 Data Types

Python supports several data types, including:

  • Numbers (int, float, complex)
  • Strings (str)
  • Lists (list)
  • Tuples (tuple)
  • Dictionaries (dict)
  • Sets (set)
  • Booleans (bool)

4.3 Operators

Python supports various operators, including:

  • Arithmetic operators: +, -, *, /, %, **, //
  • Comparison operators: ==, !=, >, <, >=, <=
  • Logical operators: and, or, not
  • Assignment operators: =, +=, -=, *=, /=, %=, **=, //=

4.4 Conditional Statements

Conditional statements allow you to execute code based on certain conditions. Here's an example:

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

Output:

x is greater than 5

5. Loops in Python

5.1 For Loop

The for loop is used to iterate over a sequence (e.g., a list, tuple, dictionary, set, or string). Here's an example:

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)
                

Output:

apple
banana
cherry

5.2 While Loop

The while loop continues to execute as long as the condition is true. Here's an example:

i = 1
while i < 6:
    print(i)
    i += 1
                

Output:

1
2
3
4
5

6. Functions in Python

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

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

greet("Alice")
greet("Bob")
                

Output:

Hello, Alice
Hello, Bob

7. Importing Modules

Python has a rich standard library that you can import and use in your programs. For example, you can use the math module to perform mathematical operations:

import math

print(math.sqrt(16))
                

Output:

4.0

8. Conclusion

In this tutorial, we covered the basics of Python, including variables, data types, operators, conditional statements, loops, functions, and importing modules. Python is a versatile language with a simple syntax, making it a great choice for beginners and experienced programmers alike.