Introduction to Python
Programming
• Python was created by Guido van Rossum and
released in 1991.
• It emphasizes readability and simplicity.
• Topics Covered:
• - Python Interpreter/Shell
• - Indentation
• - Identifiers and Keywords
• - Literals, Numbers, and Strings
Python Interpreter Overview
• The Python interpreter executes code line-by-
line.
• It can be accessed via command line or IDEs
like IDLE, PyCharm.
• Useful for testing and debugging.
Using the Python Shell
• The shell (REPL) allows interactive coding.
• Examples:
• >>> 2 + 2
• 4
• >>> print('Hello')
• Hello
Indentation in Python
• Python uses indentation to define blocks of
code.
• This replaces braces used in other languages.
• Example:
• def greet():
• print('Hello')
• print('Welcome')
Indentation Errors
• Incorrect indentation leads to syntax errors.
• Example:
• def greet():
• print('Hello') # Error: expected an indented
block
Identifiers in Python
• Identifiers are names for variables, functions,
etc.
• Examples:
• user_name = 'Alice'
• total = 100
Keywords in Python
• Keywords are reserved words.
• Cannot be used as identifiers.
• Examples: if, else, while, for, def, return
Identifiers vs Keywords
• Comparison:
• Identifiers: user-defined names
• Keywords: predefined by Python
• Use 'keyword' module to list all keywords.
Literals in Python
• Literals are fixed values.
• Types:
• - Numeric
• - String
• - Boolean
• - Special (None)
Numeric Types
• Examples:
• x = 10 # int
• y = 3.14 # float
• z = 2 + 3j # complex
String Literals
• Strings are sequences of characters.
• Examples:
• name = 'Alice'
• greeting = "Hello"
• multiline = '''This is
• a multiline string'''
Arithmetic Operators
• Used for mathematical operations.
• Examples:
• a + b, a - b, a * b, a / b, a % b
Relational Operators
• Used for comparisons.
• Examples:
• a == b, a != b, a > b, a < b
Boolean Operators
• Used for logical operations.
• Examples:
• a and b, a or b, not a
Assignment and Ternary Operators
• Assignment: a = 5, a += 1
• Ternary: result = a if a > b else b
Bitwise Operators
• Operate on binary representations.
• Examples:
• a & b, a | b, a ^ b, ~a, a << 1, a >> 1
Quiz: Python Interpreter/Shell
• What does the Python interpreter do?
• - Compiles Python code to machine code
• - Executes Python code line by line
• - Stores Python code in a database
• - Translates Python code to Java
• Correct Answer: Executes Python code line by
line
Quiz: Indentation
• Why is indentation important in Python?
• - It makes code look pretty
• - It defines blocks of code
• - It is optional
• - It helps in commenting
• Correct Answer: It defines blocks of code
Quiz: Identifiers and Keywords
• Which of the following is a valid identifier?
• - for
• - 123name
• - user_name
• - def
• Correct Answer: user_name
Quiz: Literals, Numbers, and
Strings
• Which of the following is a string literal in
Python?
• - 42
• - 3.14
• - 'Hello'
• - True
• Correct Answer: 'Hello'
Quiz: Operators and Expressions
• Which operator is used for exponentiation in
Python?
• -^
• - **
• -*
• - //
• Correct Answer: **
Embedded Python Program:
Calculator
• # Simple Calculator Program
• def add(x, y):
• return x + y
• def subtract(x, y):
• return x - y
• def multiply(x, y):
• return x * y
• def divide(x, y):
• if y != 0:
Program Explanation - Part 1
• This is a simple calculator program that
performs basic arithmetic operations.
Program Explanation - Part 2
• Functions are defined for each operation: add,
subtract, multiply, divide.
Program Explanation - Part 3
• The divide function includes error handling for
division by zero.
Program Explanation - Part 4
• The user is prompted to select an operation
and input two numbers.
Program Explanation - Part 5
• Based on the user's choice, the corresponding
function is called and the result is displayed.
Program Explanation - Part 6
• If the input is invalid, an error message is
shown.
History of Python - Part 1
• Python was conceived in the late 1980s by
Guido van Rossum at Centrum Wiskunde &
Informatica (CWI) in the Netherlands.
• It was intended to be a successor to the ABC
language, capable of exception handling and
interfacing with the Amoeba operating
system.
History of Python - Part 2
• Python 2.0 was released in 2000, introducing
list comprehensions and garbage collection.
• Python 3.0, released in 2008, was a major
revision that was not backward compatible.
• Today, Python is one of the most popular
programming languages in the world.
Special Properties of Python
• - Interpreted language
• - Dynamically typed
• - High-level syntax
• - Extensive standard library
• - Supports multiple programming paradigms
(procedural, object-oriented, functional)
Advantages and Disadvantages of
Python
• Advantages:
• - Easy to learn and use
• - Large community and support
• - Versatile and powerful
• - Great for rapid development
• Disadvantages:
• - Slower execution compared to compiled
languages
Calculator Program Flowchart