0% found this document useful (0 votes)
11 views30 pages

Pwp Unit 1 Notes

The document outlines a programming course in Python, detailing the evaluation structure and unit weightage. It covers fundamental concepts such as Python's features, building blocks, data types, and basic I/O operations. Additionally, it provides examples of Python syntax, including variable definitions, comments, and string manipulation techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views30 pages

Pwp Unit 1 Notes

The document outlines a programming course in Python, detailing the evaluation structure and unit weightage. It covers fundamental concepts such as Python's features, building blocks, data types, and basic I/O operations. Additionally, it provides examples of Python syntax, including variable definitions, comments, and string manipulation techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Subject:Programming with python

Sub-code:22616
TH-ESE: 70
Class test 1&2 Avg: 20
Micro-project: 10
PR-PA: 25
PR-ESE: 25
Total: 150
Unit Title Weightage

Introduction and Syntax of python program 08

Python operator and control flow statements 10

Data structure in python 14

Python function, modules and packages 14

Object oriented programming in python 12

File I/O handling and Exception Handling 12

Total 70
Unit-I:Introduction and Syntax of python
program

• 1.1 Features of python:


1.interactive
2.Object-Oriented
3.Interpreted
4.Platform Independent
1.Interactive python
• Python is interactive. When a Python statement is entered, and is followed by
the Return key, if appropriate, the result will be printed on the screen,
immediately, in the next line. This is particularly advantageous in the debugging
process. In interactive mode of operation, Python is used in a similar way as the
Unix command line or the terminal.
• You can actually sit at a Python prompt and interact with the interpreter directly
to write your programs
2.Object-Oriented
• Oop provide a new angle to the existing procedural language . In OOP attributes
and methods can be collaborated into a structure class. This class can be
accessed using object. Python is not only an OOP but also beatutiful mix of
various programming features
• Python supports object oriented language and concepts of classes and objects
come into existence.
3.Interpreted
• Python is called an interpreted language because it executes code
logic directly, line by line, without the need for a separate compilation
step. In methods to compiled languages like C or C++, where the
source code is translated into machine code before execution, Python
code is translated into intermediate code by the Python interpreter
• Python is an interpreted language i.e. interpreter executes the code
line by line at a time. This makes debugging easy and thus suitable for
beginners
Python is Platform Independent
• Python can run equally on different platforms such as Windows,
Linux, Unix and Macintosh etc. So, we can say that Python is a
portable language
1.2 Python building blocks
• Indetifiers
• Keywords
• Variable
• Comments
• Indentation
Indetifiers
• To process the data which stored in memory, the memory space must
be given some distinct / unique names and these unique names
makes us to identify the values present in memory and these distinct
names are called Identifiers.
• identifiers are also called Variables because identifier values can be
changed during program execution.
• Hence in python, all values must be stored in memory in the form
identifiers / Variables.
keywords
• Python keywords are special reserved words that have specific
meanings and purposes and can’t be used for anything but those
specific purposes
• >>> import keyword
• >>> print(keyword.kwlist)
• ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break',
'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from',
'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass',
'raise', 'return', 'try', 'while', 'with', 'yield']
Variable
• Def of Variable:
• A variable one of the identifier, whose value can be changed during execution of the program.
• Rules for using Identifiers / Variables in Python
• 1) The variable name is a combination of alphabets , digits and special symbol under
• score ( _ ).
• 2) The First letter of Variable Name must starts with either alphabet or underscore ( __ ).
• Examples:
• >>> a=10 valid
• >>> _a=10 valid
• >>> _1=20 valid
• >>> A=10 valid
• >>> 1=10 invalid
• >>> $a=10 invalid
• >>> +a=10 invalid
• >>> a101=10 valid
• >>>_$=40----invalid
4) We should not use Keywords as variable names (because keywords are the
reserved words and gives some specific meaning.
• >>>if =12 not valid (if is keyword)
• >>> else=10 not valid

5) Variable names are case sensitive


Example:-
>>> age=27
>>> AGE=20
Comments:
• Comments in python Comments are descriptions that help programmers better understand the
intent and functionality of the program. They are completely ignored by the Python interpreter
1.Single line comments:
In Python, we use the hash symbol # to write a single-line comment.
#This is single line comment example
#comment.py
2.Multi-line comments:
In python we use the single triple quant and triple double quant to write a multi line comments
a) triple single quotes
' ' ' This is multi line comment example
comment.py ' ' '
#OR
b) triple double quotes
" " " This is multi line comment example
comment.py " " "
Indentation:-
• In Python, indentation is used to define blocks of code.
• Indentation is achieved using whitespace (spaces or tabs) at the beginning of
each line.
if 10 > 5:
print("This is true!")
print("I am tab indentation")

print("I have no indentation")


1.3 Python environment setup-
installation and working of IDE
• Step 1): Go to the python official website at https://www.python.org
• Step 2): Choose the latest version of Python releases for Windows.
• Step 3): After choosing the correct released version, Click on the download
Python.
• Step 4): Click on Install now, and you can add python.exe path.
• Step 5) : After the installation completed , You will find python is installed in your
system .
Python I/O function
• Python Output
• In Python, we can simply use the print() function to print output
Eg: print('Python is powerful')
# Output: Python is powerful
• Python Input
• Python uses Input () functions to take input from keyboard. This function takes
value from keyboard and returns as a string.
Eg: str = Input (“Enter your Company Name :”)
Print (str)
• Python integer input
• taking integer input we have to type cast those inputs into integers by using
Python built-in int() function.
Eg: n = int(input("Enter the number : "))
Take input for single charater

• str=input(“enter a value for str”)


• Print(“str is:”,+[0])

1.4 Running simple python script to display welcome message


Print(“welcome”)
1.5 Python data-types
• Number
1: int
2:float
3:complex
• String
• Tuple
• List
• Dictionary
Number data types:
Integers – This value is represented by int class. It contains positive or negative
whole numbers (without fractions or decimals). In Python, there is no limit to
how long an integer value can be.
a=5
print("Type of a: ", type(a))
Float – This value is represented by the float class. It is a real number with a
floating-point representation. It is specified by a decimal point.
b = 5.0
print("Type of b: ", type(b))
Complex Numbers – A complex number is represented by a complex class. It is
specified as (real part) + (imaginary part)j . For example – 2+3j
c = 2 + 4j
print("Type of c: ", type(c))
Python Tuple data type
• 'tuple' is one of the pre-defined class
• An object of tuple allows us to store multiple values of same type or different type or both types.
• An object of tuple type allows us to organize both unique and duplicate values.
• The elements of tuple must be enclosed within Braces ( ) and the values must be separated by
comma (,)
• On the object of tuple , we can perform both indexing and slicing.
• An object of tuple is Immutable.
• To convert one type values into tuple type values , we use tuple()
• How to create tuble or empty tuble
a=(1,2,1,”hello”,10.20)
print(a)
>>>tp=() --------empty tuple
(or)
>>>a=tuple()
Python List data type
• 'list' is one of the pre-defined class
• An object of list allows us to store multiple values of same type or different type or both types.
• An object of list type allows us to organize both unique and duplicate values.
• The elements of list must be enclosed within Square Brackets [ ] and the values must be separated
by comma (,).
• On the object of list , we can perform both indexing and slicing.
• An object of list is Mutable.
• To convert one type values into list type values , we use list()
• How to create list or empty list
a=[1,2,1,”hello”,10.20]
print(a)
>>>li=() --------empty list
(or)
>>>a=list()
>>>print(a)---- () ----empty list
Python dictionary data type
• This data type allows us to store the data in the form of (key,value)
• In (Key,value), the values of Key represents Unique, the values of Value represents may or
may not be Unique.
• The elements dict object must be enlcosed within curly braces{ } and whose (key,value)
must separated by colon(:) and terminated by comma( ,).
• To convert one type of values into dict type we use dict() and it can also use to create
empty dict object.
• An object of dict is mutable.
• Syntax for empty dict:
• dictobj={} (or)
• dictobj=dict()
• Syntax for creating non-empty dict:
dictobj={keyname-1 : val1, keyname-2 : val2,...keyname-n : val-n}
String Data Type
• A string is a collection of one or more characters enclosed in a single quote,
double-quote, or triple-quote.
• there is no character data type Python, a character is a string of length one. It is
represented by str class.
• s = "GfG"
• print(s)
• Multi-line Strings
• If we need a string to span multiple lines then we can use triple quotes (”’ or
“””).
• s = """I am Learning
• Python String on GeeksforGeeks"""
• print(s)
Operations on string data:
• On String object data, we can perform two types of operations.
1) string indexing
2) string slicing
string indexing:
• The process of obtaining a particular character from given string by passing valid
index is called String Indexing.
• Python programming allows both forward and backward indexing
Syntax:- strobj[index]
here index can be either +ve / -ve valid index.
if the index value is invalid, we get IndexError
string slicing
• The process obtaining range of characters / sub string from the given main String
is called String Slicing.
• in Python, you perform slicing using the colon : operator.
Syntax of String Slicing in Python
substring = s[start : end : step]
• Start(Optional Parameter): Start index of the sequence where slicing is to be
start. Default Value of Start in Slice Function is 0 i.e. the slicing will start from the
starting of the string.
• Stop(optional Parameter): End index of the sequence where slicing to be end.
• Step (optional Parameter): - Integer value which determines the increment
between each index for slicing.
Example

1. b = "Hello, World!"
print(b[2:5])

2. b = "Hello, World!"
print(b[:5])

3. b = "Hello, World!"
print(b[2:])

4. b = "Hello, World!"
print(b[-5:-2])
s = "welcome to scaler"
# reversing complete string
s1 = s[::-1]
print("s1:", s1)
# reversing complete string by stepping to every 2nd element
s2 = s[::-2]
print("s2:", s2)
# reversing from 10th index until start, 'stop' index here automatically will be till starting of the string
s3 = s[10::-1]
print("s3:", s3)
# reversing from end until 10th index, 'start' index will automatically be the first element
s4 = s[:10:-1]
print("s4:", s4)
# reversing from 16th index till 10th index
s5 = s[16:10:-1]
print("s5:", s5)
# this will return empty, as we're not reversing here. But NOTE that this 'start' cannot be greater than ‘stop’ until & unless
we're reversing
s6 = s[11:2]
print("s6:", s6)
# reversing from 14th index from the end until 4th index from the end.
s7 = s[-4:-14:-1]
print("s7:", s7)
Output:
s1: relacs ot emoclew
s2: rlc teolw
s3: ot emolew
s4: relacs
s5: relacs
s6:
s7: acs ot emo

You might also like