Introduction to Python
Faculty : Moushreeta Debroy
Programme : Python
Department : BCA
Semester : 1
Module : 1
What is Python?
• Python is a high-level, interpreted, general-purpose programming
language known for its readability and simplicity.
• Python is an easy language to learn because of its simple syntax
• Python can be used for simple tasks such as plotting or for more
complex tasks like machine learning
History of Python
• 1989: Python conceived by Guido van Rossum.
• 1991: Python 0.9.0 released, included features like exception handling
and functions.
• 2000: Python 2.0 introduced list comprehensions and garbage
collection.
• 2008: Python 3.0 released with improvements, including better
Unicode support.
Why Learn Python?
• Easy to Learn and Use: Simple syntax resembles natural language.
• Versatility: Used in web development, data science, automation, etc.
• Extensive Libraries and Frameworks: Like NumPy, pandas, matplotlib,
seaborn etc.
• Community Support: Large, active community and plenty of learning
resources.
• Example Use Cases: Data analysis with pandas, web development
with Flask.
IDEs for Python
• PyCharm
• VS Code
• Jupyter Notebook
• Spyder
• IDLE (built-in)
Python as an Interpreter, Not a Compiler
Interpreter vs Compiler:
• Interpreter: Executes code line by line, translating each line into
machine code and running it immediately.
• Compiler: Translates the entire code into machine code at once,
creating an executable file before running.
Python as Interpreter:
• Python is an interpreted language, meaning the Python interpreter
processes each line of code one at a time.
• This allows for immediate execution and feedback, making it great for
scripting, rapid prototyping, and debugging.
Advantages of Interpretation in Python:
• Ease of Debugging: Errors are reported immediately, and debugging
can be done interactively.
• Platform Independence: Python code can run on any machine with a
Python interpreter, without needing platform-specific compilation.
• Flexibility: Python supports dynamic typing and can execute code
dynamically at runtime.
Disadvantages of Interpretation in Python:
• Speed: Interpreted languages like Python tend to be slower than
compiled languages because they process code at runtime.
• Performance: The line-by-line execution means that interpreted
programs may have higher runtime overhead.
Basic Syntax and Structure
• Python uses indentation to define
code blocks.
• No need for semicolons to end
statements.
• Comments start with # and are
ignored by the interpreter.
Example
Comments
• Single-line comments: # This is a
comment
• Multi-line comments: '''This is a
comment''‘
First Python program:
```
print("Hello, World!")
```
.
• Python uses indentation to define blocks of code.
Example:
```
if 5 > 2:
print("Five is greater than two!")
```
Variables in Python
What are Variables?
Containers for storing data values
• No Need to declare a variable type
• Python infers the type automatically
#Declaring Variables
x = 5
name = "John“
Naming Variables
Rules for Naming
• Must start with a letter or underscore (_)
• Can contain letters, numbers, and underscores
• Case-sensitive (name and Name are different)
• Examples:
• Valid: age, first_name, _count
• Invalid: 2name, first-name, first name
Data Types
• x = 10 # int
• y = 3.14 # float
• name = "Alice" # str
• is_valid = True # bool
Python Data Types Overview
List
• Ordered: Elements have a specific order.
• Mutable: Can be changed (add, remove, modify elements).
• Duplicates Allowed: Lists can contain duplicate elements.
Example:
my_list = [1, 2, 3, "apple", 3]
Python Data Types Overview
Set
• Unordered: Elements do not have a specific order.
• Mutable: Can add, remove, or modify elements.
• No Duplicates: Sets do not allow duplicate elements.
Example:
my_set = {1, 2, 3, "apple", 3}
Python Data Types Overview
Tuple
• Ordered: Elements have a specific order.
• Immutable: Cannot be changed once created (no adding, removing, or
modifying elements).
• Duplicates Allowed: Tuples can contain duplicate elements.
Example:
my_tuple = (1, 2, 3, “apple”, 3)
Python Data Types Overview
Dictionary
• Key-Value Pairs: Each element is stored as a key-value pair.
• Mutable: Can add, remove, or change key-value pairs.
• No Duplicates: Keys must be unique, but values can be duplicated.
Example:
my_dict = {"name": "John", "age": 25, "city": "New York"}
Working with Strings
String Operations
• Concatenation: "Hello " + "World!“ #Output :"Hello World!"
• Repetition: "Hello" * 3 #Output:"HelloHelloHello"
String Methods
• upper(), lower(), len()
Example:
greeting = "Hello"
print(greeting.upper())
# Output: HELLO
Numbers in Python
Integers and Floats
• Integer: 5, -10
• Float: 3.14, -0.001
Basic Arithmetic
• Addition: 5 + 2
• Subtraction: 5 - 2
• Multiplication: 5 * 2
• Division: 5 / 2
Boolean Data Type
Booleans
• Represents True or False
Boolean Expressions
• Comparison operators: ==, !=, >, <
Converting Data Types
• int(), float(), str()
Example:
num_str = "123“
num_int = int(num_str)
print(type(num_int))
# Output: <class 'int'>
Type Conversion
Basic Operators
• Arithmetic Operators: +, -, *, /, // (floor division), % (modulus),
** (exponentiation).
• Assignment Operators: =, +=, -=, *=, /=
• Comparison Operators: ==, !=, >, <, >=, <=
• Logical Operators: and, or, not
Example:
• a= 5
• b = 2
• result = a + b # Addition
• is_greater = a > b # Comparison
Arithmetic Operators
Example:
a = 10
b = 3
print(a % b)
# Output: 1
print(a ** b)
# Output: 1000
Assignment Operators
x = 5
x += 3 # Equivalent to x = x + 3
x -= 3 # Equivalent to x = x - 3
print(x)
# Output:8
Comparison and Logical Operators
• Comparison Operators
x = 5
y = 10
print(x == y) # Output: False
print(x < y) # Output: True
• Logical Operators
print(True and False) # Output: False
print(True or False) # Output: True
print(not True) # Output: False
Exercise 1: Simple Calculator
• Write a Python program that asks the user to input two numbers and then performs the
following operations:
• Addition
• Subtraction
• Multiplication
• Division
Exercise 2: Greeting the User
• Write a Python program that:
• Takes the user's first name and last name as input.
• Greets them using their full name in a nicely formatted message.
Exercise 3: Even or Odd Checker
• Write a Python program that:
• Asks the user to input an integer.
• Checks whether the number is even or odd and prints the result.
Exercise 4: Word Manipulator
• Write a Python program that:
• Takes a string input from the user.
• Prints the length of the string.
• Converts the string to uppercase and prints the result.
• Repeats the string 3 times and prints the result.
Thank you

python_module_.................................................................

  • 1.
    Introduction to Python Faculty: Moushreeta Debroy Programme : Python Department : BCA Semester : 1 Module : 1
  • 2.
    What is Python? •Python is a high-level, interpreted, general-purpose programming language known for its readability and simplicity. • Python is an easy language to learn because of its simple syntax • Python can be used for simple tasks such as plotting or for more complex tasks like machine learning
  • 3.
    History of Python •1989: Python conceived by Guido van Rossum. • 1991: Python 0.9.0 released, included features like exception handling and functions. • 2000: Python 2.0 introduced list comprehensions and garbage collection. • 2008: Python 3.0 released with improvements, including better Unicode support.
  • 5.
    Why Learn Python? •Easy to Learn and Use: Simple syntax resembles natural language. • Versatility: Used in web development, data science, automation, etc. • Extensive Libraries and Frameworks: Like NumPy, pandas, matplotlib, seaborn etc. • Community Support: Large, active community and plenty of learning resources. • Example Use Cases: Data analysis with pandas, web development with Flask.
  • 6.
    IDEs for Python •PyCharm • VS Code • Jupyter Notebook • Spyder • IDLE (built-in)
  • 7.
    Python as anInterpreter, Not a Compiler Interpreter vs Compiler: • Interpreter: Executes code line by line, translating each line into machine code and running it immediately. • Compiler: Translates the entire code into machine code at once, creating an executable file before running.
  • 8.
    Python as Interpreter: •Python is an interpreted language, meaning the Python interpreter processes each line of code one at a time. • This allows for immediate execution and feedback, making it great for scripting, rapid prototyping, and debugging.
  • 9.
    Advantages of Interpretationin Python: • Ease of Debugging: Errors are reported immediately, and debugging can be done interactively. • Platform Independence: Python code can run on any machine with a Python interpreter, without needing platform-specific compilation. • Flexibility: Python supports dynamic typing and can execute code dynamically at runtime.
  • 10.
    Disadvantages of Interpretationin Python: • Speed: Interpreted languages like Python tend to be slower than compiled languages because they process code at runtime. • Performance: The line-by-line execution means that interpreted programs may have higher runtime overhead.
  • 11.
    Basic Syntax andStructure • Python uses indentation to define code blocks. • No need for semicolons to end statements. • Comments start with # and are ignored by the interpreter.
  • 12.
    Example Comments • Single-line comments:# This is a comment • Multi-line comments: '''This is a comment''‘ First Python program: ``` print("Hello, World!") ``` .
  • 13.
    • Python usesindentation to define blocks of code. Example: ``` if 5 > 2: print("Five is greater than two!") ```
  • 14.
    Variables in Python Whatare Variables? Containers for storing data values • No Need to declare a variable type • Python infers the type automatically #Declaring Variables x = 5 name = "John“
  • 15.
    Naming Variables Rules forNaming • Must start with a letter or underscore (_) • Can contain letters, numbers, and underscores • Case-sensitive (name and Name are different) • Examples: • Valid: age, first_name, _count • Invalid: 2name, first-name, first name
  • 17.
    Data Types • x= 10 # int • y = 3.14 # float • name = "Alice" # str • is_valid = True # bool
  • 18.
    Python Data TypesOverview List • Ordered: Elements have a specific order. • Mutable: Can be changed (add, remove, modify elements). • Duplicates Allowed: Lists can contain duplicate elements. Example: my_list = [1, 2, 3, "apple", 3]
  • 19.
    Python Data TypesOverview Set • Unordered: Elements do not have a specific order. • Mutable: Can add, remove, or modify elements. • No Duplicates: Sets do not allow duplicate elements. Example: my_set = {1, 2, 3, "apple", 3}
  • 20.
    Python Data TypesOverview Tuple • Ordered: Elements have a specific order. • Immutable: Cannot be changed once created (no adding, removing, or modifying elements). • Duplicates Allowed: Tuples can contain duplicate elements. Example: my_tuple = (1, 2, 3, “apple”, 3)
  • 21.
    Python Data TypesOverview Dictionary • Key-Value Pairs: Each element is stored as a key-value pair. • Mutable: Can add, remove, or change key-value pairs. • No Duplicates: Keys must be unique, but values can be duplicated. Example: my_dict = {"name": "John", "age": 25, "city": "New York"}
  • 22.
    Working with Strings StringOperations • Concatenation: "Hello " + "World!“ #Output :"Hello World!" • Repetition: "Hello" * 3 #Output:"HelloHelloHello" String Methods • upper(), lower(), len() Example: greeting = "Hello" print(greeting.upper()) # Output: HELLO
  • 23.
    Numbers in Python Integersand Floats • Integer: 5, -10 • Float: 3.14, -0.001 Basic Arithmetic • Addition: 5 + 2 • Subtraction: 5 - 2 • Multiplication: 5 * 2 • Division: 5 / 2
  • 24.
    Boolean Data Type Booleans •Represents True or False Boolean Expressions • Comparison operators: ==, !=, >, <
  • 25.
    Converting Data Types •int(), float(), str() Example: num_str = "123“ num_int = int(num_str) print(type(num_int)) # Output: <class 'int'> Type Conversion
  • 26.
    Basic Operators • ArithmeticOperators: +, -, *, /, // (floor division), % (modulus), ** (exponentiation). • Assignment Operators: =, +=, -=, *=, /= • Comparison Operators: ==, !=, >, <, >=, <= • Logical Operators: and, or, not
  • 27.
    Example: • a= 5 •b = 2 • result = a + b # Addition • is_greater = a > b # Comparison
  • 28.
    Arithmetic Operators Example: a =10 b = 3 print(a % b) # Output: 1 print(a ** b) # Output: 1000
  • 29.
    Assignment Operators x =5 x += 3 # Equivalent to x = x + 3 x -= 3 # Equivalent to x = x - 3 print(x) # Output:8
  • 30.
    Comparison and LogicalOperators • Comparison Operators x = 5 y = 10 print(x == y) # Output: False print(x < y) # Output: True • Logical Operators print(True and False) # Output: False print(True or False) # Output: True print(not True) # Output: False
  • 31.
    Exercise 1: SimpleCalculator • Write a Python program that asks the user to input two numbers and then performs the following operations: • Addition • Subtraction • Multiplication • Division Exercise 2: Greeting the User • Write a Python program that: • Takes the user's first name and last name as input. • Greets them using their full name in a nicely formatted message.
  • 32.
    Exercise 3: Evenor Odd Checker • Write a Python program that: • Asks the user to input an integer. • Checks whether the number is even or odd and prints the result. Exercise 4: Word Manipulator • Write a Python program that: • Takes a string input from the user. • Prints the length of the string. • Converts the string to uppercase and prints the result. • Repeats the string 3 times and prints the result.
  • 33.