Unit1
1. Introduction to Python Programming
Course Code: 10211CS213
2. Course Objectives
Understand the basic program structure and elements of Python Language.
Learn about file manipulation, string manipulation, and regular expressions.
Grasp concepts of exception handling and multithreading in Python.
Use Python modules for data analysis, gaming, and UI design.
3. Why Programming?
Turns ideas (games, applications) into executable instructions for the computer.
Programming enables the realization of software-related concepts.
4. Why Python is Popular?
User-friendly applications
Free libraries and community support
5. Advantages of Python
Easy to use and learn.
Flexibility in various applications.
Efficient, fast, and reliable.
Widely applicable in web development, data analysis, machine learning, AI, automation, and scientific computing.
Robust libraries available for numerous projects.
6. Code Comparisons
C Example:
#include <stdio.h> int main() { printf("Hello, World!"); return 0; }Python Example:
print("Hello, World!")
7. What is Python?
Python is a general-purpose, high-level, interpreted language known for its easy syntax and dynamic semantics.
Created by Guido Van Rossum in 1989.
8. Semantic Rules in Python
Static Semantics: Rules checked before execution.
Dynamic Semantics: Rules checked during execution.
9. Features of Python
Embeddable & open-source
Object-oriented programming (OOP)
Extensible libraries
Simplicity and portability
10. Python Basics
10.1 Variables
Variables are containers for storing data. No specific command for declaration in Python.
Example:
x = 5 y = "John" print(x) print(y)
10.2 Data Type Flexibility
Variables can change type after assignment.
Example:
x = 4 # int x = "Rajesh" # now str print(x)
10.3 Type Function
Use
type()to check the data type of a variable.x = 5 y = "John" print(type(x)) print(type(y))
10.4 Changing Data Types
Example to change from int to float and vice versa:
num1 = 10 num1_float = float(num1) print(type(num1_float)) num2 = 2.5 num2_int = int(num2) print(type(num2_int))
10.5 Strings
Strings can be declared using single or double quotes.
Case-sensitive variables:
a = 4 A = "Sally" # A does not overwrite a
10.6 Naming Variables
Criteria:
Must start with a letter or underscore
Cannot start with a number
Can contain alpha-numeric characters and underscores
Case-sensitive (age, Age, AGE are different variables)
11. Data Types in Python
Integer: Whole numbers, no limit on size.Example:
x = 5Float: Real numbers, can be scientific notation.Example:
x = 5.5Complex Numbers: Two values, real and imaginary parts.Example:
x = 2 + 3j
11.1 Dictionary
Stores data in key:value pairs, ordered and changeable without duplicates.
dict1 = {"brand": "maruthi", "model": "Alto", "year": 1964} print(dict1)
11.2 Boolean
Represents one of two values: True or False.
print(10 > 9) print(10 == 9) print(10 < 9)
11.3 Sets
Unordered, unchangeable collections without duplicate values.
myset = {"apple", "banana", "cherry"} print(myset)
11.4 Sequence Data Types
Containers: List (mutable), Tuple (immutable), String (immutable).
12. Python Lists
12.1 Creation
Ordered and changeable; allow duplicates.
thislist = ["apple", "banana", "cherry"] print(thislist)
12.2 Methods of Lists
Add an Item:
append().Remove an Item:
remove().Remove by Index:
pop().
13. Tuples
Immutable, ordered collection.
thistuple = ("apple", "banana", "cherry")
print(thistuple)14. Strings
Immutable sequence of characters.
Accessing characters via indexing, supports negative index for reverse.
15. Python Operators
Types: Arithmetic, Assignment, Comparison, Logical, Identity, Membership, Bitwise.
16. Control Statements
Used to control code execution flow: Conditional, Looping, and Branching statements.
16.1 Conditional Statements
If Statement: Executes block if condition is true.
If-else Statement: Executes one of two blocks based on condition.
Nested if Statements: Complex conditions.
16.2 Looping Statements
While Loop: Repeats as long as the condition is true.
For Loop: Iterates over a sequence.
17. Functions in Python
Enclosed block of statements for specific tasks, enhancing readability and reusability.
17.1 Defining a Function
Use
defkeyword.Example to create and call a function.
17.2 Function Arguments
Accepts arguments and can return values.
17.3 Lambda Functions
Anonymous functions for quick operations.
18. Python Modules
Files containing code for reuse.
18.1 Importing
Use
importstatement to include a module in your code.
18.2 Packages
Collection of modules, including initialization via
__init__.py.
19. Object-Oriented Programming
19.1 Classes and Objects
Classes are blueprints for creating objects.
Instance methods and attributes for each object.
20. Iterators and Generators
Iterator: Object for iterating over values.
Generator: Function that returns an iterator using the
yieldkeyword.