Lecture 1: Introduction to Python

Introduction to Python

Python is a high-level, interpreted programming language known for its simple and readable syntax. Created in 1991 by Guido van Rossum, Python was designed with code readability and ease of use in mind.

Key Features:

Readable Syntax

Clean and easy-to-understand code structure with significant whitespace.

Dynamic Typing

Variables are dynamically typed, eliminating the need for explicit type declarations.

Multi-paradigm

Supports object-oriented, functional, and procedural programming styles.

Standard Library

Comes with a large standard library for various programming tasks.

Did you know? Python was named after the British comedy group Monty Python, not the snake!

Your First Python Program

# This is a comment. It will not be executed.
print("Hello, World!")  # This line prints text to the console

Output:

Hello, World!

How it works:

  • print() is a built-in Python function that displays text on the screen.
  • Text strings in Python are enclosed in either single (') or double (") quotes.
  • Comments start with # and are ignored by the Python interpreter.

Hello World Program

# This is a comment. It will not be executed.
print("Hello, World!")

Output:

Hello, World!

How does this work:

  • print() is a built-in Python function that displays text on the screen.
  • "Hello, World!" is a string, which is a sequence of text. In Python, strings are enclosed in quotes (either single ' or double ").

Indentation in Python

In Python, indentation is used to define blocks of code. It indicates to the Python interpreter that a group of statements belongs to the same block. All statements with the same level of indentation are treated as part of the same code block. Indentation is created using whitespace at the beginning of each line, and the commonly accepted convention is to use four spaces per indentation level.

print("I have no Indentation")
   print("I have tab Indentation ")

Output:

ERROR!
Traceback (most recent call last):
  File "", line 2
    print("I have tab Indentation ")
IndentationError: unexpected indent

Explanation:

  • The first print statement has no indentation, so it is correctly executed.
  • The second print statement has tab indentation, but it doesn't belong to a new block of code. Python expects the indentation level to be consistent within the same block. This inconsistency causes an IndentationError.

Python Applications

Python's versatility makes it one of the most popular programming languages across various domains. Here's how Python is being used in the real world:

YouTube

Powers video streaming, API services, and backend infrastructure.

Instagram

Handles millions of users with Django, a Python web framework.

Spotify

Uses Python for data analysis and backend services.

Dropbox

Built its desktop client and server infrastructure with Python.

Python in Different Domains

Web Development

  • Django - Full-featured web framework
  • Flask - Lightweight and flexible microframework
  • FastAPI - Modern, fast web framework for APIs

Data Science & AI

  • Pandas - Data manipulation and analysis
  • NumPy - Numerical computing
  • TensorFlow/PyTorch - Machine learning frameworks

Automation

  • Scripting repetitive tasks
  • Web scraping with BeautifulSoup/Scrapy
  • Task automation with Python scripts

Advantages of Python

Rich Ecosystem

  • Extensive standard library for common tasks
  • Over 300,000 packages in PyPI (Python Package Index)
  • Strong community support and documentation

Beginner Friendly

  • Simple and readable syntax
  • Gentle learning curve
  • Great first programming language

Rapid Development

  • Dynamically typed
  • Automatic memory management
  • Requires fewer lines of code

Cross-Platform

  • Runs on Windows, macOS, Linux
  • Write once, run anywhere
  • Large community support

Things to Consider

Performance

While Python is generally fast enough for most applications, it may not be the best choice for performance-critical applications or system-level programming.

Mobile Development

Python is not commonly used for native mobile app development, though frameworks like Kivy and BeeWare are available.

Memory Usage

Python can consume more memory compared to languages like C or Java, which might be a consideration for memory-constrained environments.

Test Your Knowledge

1. What is the output of the following code?

print("Hello" + "Python")

2. Which of the following is NOT a valid Python comment?

3. What is the correct way to create a block of code in Python?

4. Which of the following is NOT a valid Python data type?

5. What is the correct way to print "Hello, World!" in Python 3?