Introduction to Python

Welcome to the Python tutorial! In this section, we will provide a brief introduction to Python and its features.

Python is a high-level programming language known for its simplicity and readability. It is widely used for various applications such as web development, data analysis, and artificial intelligence.

By learning Python, you will gain a valuable skill that can open up numerous opportunities in the tech industry.

Python Basics

In this section, we will cover the basic concepts of Python programming language.

    Python Basics - Variables and Data Types

    In this section, we will cover the basic concepts of variables and data types in Python programming language.

    Variables

    Variables are used to store and manage data in a program. In Python, you can create a variable and assign a value to it like this:

    # Example of creating a variable
    my_variable = 42
    print(my_variable)
        

    Data Types

    Python supports various data types. Here are some common ones:

    # Examples of different data types
    integer_variable = int(input("Enter an integer: "))
    float_variable = float(input("Enter a float: "))
    string_variable = input("Enter a string: ")
    boolean_variable = bool(input("Enter a boolean (True/False): "))
    
    print(integer_variable)
    print(float_variable)
    print(string_variable)
    print(boolean_variable)
        

    Custom Print

    Try using the print function with a user-input integer:


    Output:

     
             
           

    Python Basics - Operators

    In this section, we will cover various operators in Python and how to use them.

    Arithmetic Operators

    Arithmetic operators perform mathematical operations on numeric values:

    - Addition (`+`): Adds two values together. Example: `print(5 + 3)`

    - Subtraction (`-`): Subtracts the right operand from the left operand. Example: `print(7 - 2)`

    - Multiplication (`*`): Multiplies two values. Example: `print(4 * 6)`

    - Division (`/`): Divides the left operand by the right operand. Example: `print(8 / 2)`

    - Modulus (`%`): Returns the remainder of the division of the left operand by the right operand. Example: `print(9 % 4)`

    - Exponentiation (`**`): Raises the left operand to the power of the right operand. Example: `print(2 ** 3)`

    Comparison Operators

    Comparison operators compare two values and return a Boolean result:

    - Equal to (`==`): Checks if two values are equal. Example: `print(5 == 5)` - Not equal to (`!=`): Checks if two values are not equal. Example: `print(7 != 3)` - Greater than (`>`): Checks if the left operand is greater than the right operand. Example: `print(8 > 4)` - Less than (`<`): Checks if the left operand is less than the right operand. Example: `print(3 < 6)` - Greater than or equal to (`>=`): Checks if the left operand is greater than or equal to the right operand. Example: `print(10 >= 8)` - Less than or equal to (`<=`): Checks if the left operand is less than or equal to the right operand. Example: `print(2 <= 4)`

    Logical Operators

    Logical operators perform logical operations on Boolean values:

    - AND (`and`): Returns True if both operands are True. Example: `print(True and False)`
    - OR (`or`): Returns True if at least one operand is True. Example: `print(True or False)`
    - NOT (`not`): Returns True if the operand is False, and vice versa. Example: `print(not True)`

    Bitwise Operators

    Bitwise operators perform operations on binary representations of numbers:

    - AND (`&`): Performs a bitwise AND operation. Example: `print(5 & 3)` - OR (`|`): Performs a bitwise OR operation. Example: `print(5 | 3)` - XOR (`^`): Performs a bitwise XOR (exclusive OR) operation. Example: `print(5 ^ 3)` - NOT (`~`): Performs a bitwise NOT operation. Example: `print(~5)` - Left Shift (`<<`): Shifts the bits of the left operand to the left by the number of positions specified by the right operand. Example: `print(5 << 1)` - Right Shift (`>>`): Shifts the bits of the left operand to the right by the number of positions specified by the right operand. Example: `print(5 >> 1)`

    Python Basics - Conditional Statements

    Conditional statements allow you to control the flow of your program based on certain conditions. In Python, the most common conditional statements are if, else, and elif.

    if Statement

    The if statement is used to execute a block of code only if a specified condition is true:

    age = 18
    if age >= 18:
        print("You are eligible to vote.")
    

    if-else Statement

    The if-else statement allows you to specify two blocks of code: one to be executed if the condition is true, and another if it's false:

    age = 16
    if age >= 18:
        print("You are eligible to vote.")
    else:
        print("You are not eligible to vote yet.")
    

    elif Statement

    The elif statement is used to check multiple conditions. It comes after an if statement, and only executes if the previous conditions were false:

    score = 75
    if score >= 90:
        print("Excellent!")
    elif score >= 70:
        print("Good job!")
    else:
        print("You need improvement.")
    

    Nested if Statements

    You can also nest conditional statements to check for more complex conditions:

    age = 25
    if age >= 18:
        print("You are eligible to vote.")
        if age >= 21:
            print("You can also purchase alcohol.")
    else:
        print("You are not eligible to vote yet.")
    

    Python Basics - Loops

    Loops in Python allow you to repeatedly execute a block of code. The two most common types of loops are for and while loops.

    for Loop

    The for loop is used to iterate over a sequence (such as a list, tuple, string, or range) and execute a block of code for each item in the sequence:

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

    while Loop

    The while loop is used to repeatedly execute a block of code as long as a specified condition is true:

    count = 0
    while count < 5:
        print("Count is", count)
        count += 1
    

    Loop Control Statements

    Python provides control statements to modify the execution of loops:

    - break: Terminates the loop and transfers control to the statement immediately following the loop. - continue: Skips the rest of the code inside the loop for the current iteration and moves to the next iteration.
    numbers = [1, 2, 3, 4, 5]
    for num in numbers:
        if num == 3:
            print("Found 3, breaking the loop")
            break
        print(num)
    
    numbers = [1, 2, 3, 4, 5]
    for num in numbers:
        if num == 3:
            print("Skipping 3")
            continue
        print(num)
    

    Nested Loops

    You can also nest loops, meaning one loop inside another:

    for i in range(3):
        for j in range(2):
            print(f"({i}, {j})")