Loading...

Go Back

Next page
Go Back Course Outline

Python Full Course


Introduction to Python

1. What is Python?

History: Python was created by Guido van Rossum in 1989 and was released in 1991. It was designed with simplicity and readability in mind, making it easy for new programmers to learn.

Purpose: Python is a high-level, general-purpose programming language used for web development, data analysis, machine learning, artificial intelligence, automation, and much more.

Uses: Python is used across various domains, including web development (Django, Flask), data science (Pandas, NumPy, Matplotlib), machine learning (TensorFlow, Keras), and scripting/automation.

Advantages: Python is easy to read, has rich libraries, is versatile, and open-source.

Interpreted Language: Python is an interpreted language, meaning that the code is executed line-by-line by the Python interpreter rather than being compiled into machine code.



2. Setting up a Development Environment

Installing Python: Download Python from the official website python.org and ensure to check "Add Python to PATH" during installation.

Choosing an IDE/Text Editor:

  • VS Code: Lightweight, fast, and supports Python extensions.
  • PyCharm: Full-featured IDE with rich Python-specific tools.
  • Thonny: A beginner-friendly IDE that comes pre-installed with Python.

3. Running Python Code

Interactive Shell: Type python or python3 in your terminal/command prompt to start the interactive shell.

Running Scripts: Save Python code as script_name.py and run it using the command:

python script_name.py

4. Basic Syntax

Statements: Python code is composed of statements.

Indentation: Python uses indentation to define blocks of code. Indentation is crucial and should be consistent (spaces or tabs).

if True:
                            print("Hello, Python!")  # This is correctly indented

Comments: Comments are used to explain code and are ignored by the interpreter. Python supports single-line and multi-line comments:

# This is a single-line comment
                        
                        """
                        This is a multi-line
                        comment in Python
                        """

5. Variables and Data Types

Numbers:

Integers: Whole numbers without a decimal point.

x = 10  # Integer

Floats: Numbers with a decimal point.

y = 3.14  # Float

Complex Numbers: Numbers with a real and imaginary part.

z = 5 + 2j  # Complex Number

Strings:

Strings can be defined using single or double quotes.

name = 'John'
                        greeting = "Hello, Python!"

Escape Sequences: Special characters like newline \n and tab \t.

message = "Hello\nWorld"
                        print(message)  # Output: Hello\nWorld

Raw Strings: Strings with backslashes treated as literal characters.

path = r"C:\Users\Name\Documents"

Booleans:

Python supports two boolean values: True and False.

is_active = True
                        is_logged_in = False

Type Hinting:

Python 3 introduced type hints to specify expected data types.

def add(a: int, b: int) -> int:
                            return a + b


6. Operators

Arithmetic Operators:

a = 5
                        b = 2
                        print(a + b)  # Output: 7
                        print(a ** b) # Output: 25

Assignment Operators:

a = 5
                        a += 3  # a = a + 3
                        print(a)  # Output: 8

Comparison Operators:

print(5 == 5)  # Output: True
                        print(5 != 5)  # Output: False

Logical Operators:

print(True and False)  # Output: False
                        print(True or False)   # Output: True

Bitwise Operators:

a = 5  # 0101 in binary
                        b = 3  # 0011 in binary
                        print(a & b)  # Output: 1

Identity Operators:

x = [1, 2, 3]
                        y = [1, 2, 3]
                        print(x is y)  # Output: False

Membership Operators:

fruits = ["apple", "banana", "cherry"]
                        print("apple" in fruits)  # Output: True

7. Type Conversion and Casting

Implicit Casting (Automatic Conversion):

x = 10    # Integer
                        y = 2.5   # Float
                        print(x + y)  # Output: 12.5

Explicit Casting (Manual Conversion):

x = 10.5
                        y = int(x)  # Convert float to int
                        print(y)    # Output: 10
Go Back

Next page