python minor Module I and II
Course Details
Course Code: CSC1MN102
Subject: Python Programming 1
Instructor: Dr Manjusha K K
Department: Department of Computer Application
Institution: Mar Dionysius College Pazhanji Thrissur
Introduction to Python Programming
Definition: A program is an ordered set of instructions executed by a computer to perform a specific task.
Programming Language: The language used to write this set of instructions.
Source Code: Programs written in high-level languages.
Translators
Interpreter vs Compiler:
Interpreter: Processes program statements one at a time, translating and executing consecutively; stops upon errors.
Compiler: Translates the entire source code into object code, generating error messages for encountered issues.
Python Virtual Machine
Role: The Python interpreter executes Python code, serving as an abstraction layer between Python bytecode and hardware.
Features of Python
High-level language, free and open-source.
Interpreted language, making it easy to use, case-sensitive, and portable.
Rich library providing numerous predefined functions.
Web Development
Python is widely used for developing web applications and services.
Writing and Executing Python Programs
Indentation
Python uses indentation to organize code blocks, which is essential for proper code execution and readability.
Running Python Programs
Interactive Mode: Execute single Python statements instantly at the prompt.
Script Mode: Write multiple instructions in a file with a
.pyextension for batch execution.
Comments in Python
Used for adding remarks in source code, which enhances code clarity.
Syntax: Starts with
#, ignored by the interpreter.
Identifiers and Keywords
Identifiers
Names used for variables and functions.
Rules: Must start with a letter or underscore, can include letters, digits, and underscores. Cannot be a reserved word or start with a digit.
Keywords
Reserved words with specific meanings in Python.
Variables
Represents objects stored in memory, needing assignment before use.
Python allows implicit variable declaration; no explicit type definition is needed.
Example Code
# Area of a rectangle
length = 10
breadth = 20
area = length * breadth
print(area)
# Output: 200Everything is an Object
Python treats all data types as objects with unique identities.
Use
id()function to return the identity of an object.
Data Types
Every value falls into a specific data type, identifying the kind of value it can hold and operations allowed.
Major categories include numbers, strings, lists, tuples, dictionaries, and sets.
Number Data Type
Types: Integer (
int), Float (float), Complex (complex).
Sequences and Collections
Sequence: Ordered collection of items indexed by integers.
Types include:
String: Group of characters.
List: Items stored in square brackets
[].Tuple: Similar to lists but immutable.
Mapping
Dictionary: Holds key-value pairs, enabling rapid data access.
Access values using keys.
Input and Output
Use
print()for output andinput()for gathering user input.
Import Statements
Include external modules or libraries to extend functionality.
Syntax:
import module_nameorfrom module_name import item_name.
Example
import math
print(math.sqrt(16)) # Output: 4.0Range Function
Generates a sequence of numbers, commonly used for iterating in loops.
Syntax:
range(start, stop, step)with optional parameters for range specification.
Operators
Types: Arithmetic, Assignment, Comparison, Logical, Identity, Membership, Bitwise.
Arithmetic Operators
Perform basic mathematical operations.
Assignment Operators
Assign values to variables.
Comparison Operators
Compare two values.
Logical Operators
Combine conditional statements.
Control Flow
Statements: Include if, elif, else, and nested structures.
Decision Making in Python
Built-in keywords:
if,elif,elsefor conditional statements.
Syntax Example
if condition:
# execute block
else:
# execute another blockLoops in Python
Types:
While Loop: Executes code block as long as condition is true.
For Loop: Iterates through a sequence or range.
Nested Loops: Loop inside another loop.
Control Statements
Break: Exits loop prematurely.
Continue: Skips current loop iteration.
Pass: Does nothing; a placeholder.
Nested Loops Example
for i in range(3):
for j in range(2):
print(i, j) Conclusion
Understanding these foundational concepts is crucial for efficient Python programming and grasping more advanced topics.