AR20 Python Unit-I
AR20 Python Unit-I
Python
Programming
Unit-I
• Python is Interactive
• Python is Object-Oriented
• Easy-to-learn
• Easy-to-read
• Easy-to-maintain
• Interactive Mode
• Portable
• Extendable
• Databases
• GUI Programming
• Scalable
• Education:
– Python offers an interactive environment in which to explore
procedural, functional and object oriented
approaches to problem solving.
– Its high level data structures and clear syntax make it an ideal
first language, while the large number of existing
libraries make it suitable to tackle almost any programming
tasks.
• Business Applications:
print() function:
• This means that when you create a variable you reserve some
space in memory.
• Ex: x=5
• Here x is a variable which stores value 5.
• There are several reserved words and you cannot use them
as constant or variable or any other identifier names.
• All the Python keywords contain lowercase letters only.
and def exec if not return
• Example: x = input()
• Example: print(x)
Example:
x = 123456789012345678901234567890
y=1
Print(x+y)
Output:
12345678901234
56789012345678
91
Program: Program:
n=12 n=12
print("value of n is %d"%n) m=13.56
print("value of n is %f"%n) print("va
lues are
%d,
Sample input output: %f"%
value of n is 12 (n,m))
value of n is 12.000000
Sample
input
output:
values
Python Programming Raghu Engineering College 30
are
Data Types – Restricting
Number of Decimal
• places
We can restrict the number of decimal places in floating
point values by using %.nf.
• Substitute some value in place of n.
Program:
x=15.123456789
y=15.149
print("x is %.2f
and y is %.5f"%
(x,y))
Sample input
output:
Python Programming Raghu Engineering College 31
x is 15.12 and y
Data Types - Strings
• Strings in Python are identified as a contiguous set of characters
represented in the quotation marks.
Program:
s1="hello"
s2="world"
print("first
string is
%s and
second
string is
%s"%
(s1,s2))
Slide 33
Sample
Python Programming Raghu Engineering College
Data Types - Boolean
• Boolean values are the two constant objects False and True.
• They are used to represent truth values (other values can also be
considered false or true).
Sample Sample
input input
output: output:
25 21
•Assignment Operators
•Logical Operators
•Bitwise Operators
•Membership Operators
•Identity Operators
Python Programming 40
Arithmetic Operators
x3 = [1,2,3]
y3 = [1,2,3]
print(x3 is y3) # Output: False
But x3 and y3 are lists. They are equal but not identical. Since lists
are mutable (can be changed), interpreter locates them separately
in memory although they are equal.
fruit = "banana"
print(fruit[:3]) # outputs ban
print(fruit[3:]) # outputs ana
Program: Program:
print(7//2*6) print(6*7//2)
Program:
print(2**3**2)
name = “raghu“
print(name.isalpha()) #returns true
name = “ab3cd22er“
print(name.isalpha()) #returns false
s = "28212“
print(s.isdigit()) #returns true
s = “CoMpUteR”
s.upper() # converts characters in s to “COMPUTER”
s.lower() # converts characters in s to “computer”
s = “CoMpUteR”
s.swapcase() # converts characters in s to
“cOmPuTEr”
s = “abcd”
s.islower() # returns True
s = “abcd4”
s.islower() # returns True
s = “ABCD”
s.isupper() # returns True
s = “ABCD4”
s.isupper() # returns True
s1 = “hello”
s2 = “world”
s1 + s2 #contains “helloworld”
• To get a character from it's ASCII value use the chr() function.
Example:
print(chr(65))
# Prints 'A'
70
String Handling
Functions
Python String join()
Returns a Concatenated String
string.join(str)
separator.join(iterable)
Python String strip() Removes Both Leading and Trailing spaces: string.strip()
Python String rfind() Returns the Highest Index of Substring: str.rfind(sub[, start[, end]] )
Python String rindex() Returns Highest Index of Substring: str.rindex(sub[, start[, end]] )