Python Unit I
Python Unit I
Unit I
UNIT I
PYTHON
• Python is one of the most dynamic and versatile programming
languages.
• It is a high-level programming language.
• It was created by Guido van Rossum during 1985- 1990.
• It is named after a BBC comedy series from the 1970s “Monty
Python’s Flying Circus”.
Features
Steps to Download and Install Python
3.8 for Windows
• To start, go to python.org/downloads and then click
on the button to download the latest version of
Python
Steps
• Step 2: Run the .exe file
• Next, run the .exe file that you just downloaded
/ second
// second
= x=5 x=5
+= x += 5 x=x+5
-= x -= 5 x=x-5
*= x *= 5 x=x*5
/= x /= 5 x=x/5
%= x %= 5 x=x%5
//= x //= 5 x = x // 5
**= x **= 5 x = x ** 5
Bitwise operator
OPERATOR DESCRIPTION SYNTAX
| Bitwise OR x|y
~ Bitwise NOT ~x
>>> nlist.clear()
>>> print(nlist)
[]
List methods
index() - Returns the index of the first matched
item
>>> age = 15
>>> print('%s stays at %s, his age is %i ‘ %(name,stay,age))
Ayush stays at Andheri, his age is 15
Escape Character
• To insert characters that are illegal in a string, use an escape
character.
• An escape character is a backslash \ followed by the character you
want to insert.
• An example of an illegal character is a double quote inside a string
that is surrounded by double quotes
• Text = “The cat said “Meow””
• Text = “The cat said \“Meow\””
String Functions
• len(): The len() function returns the length of a string.
• str(): This function converts any data type into a string.
Methods
• lower() and upper(): These methods return the string in lowercase
and uppercase, respectively.
• strip(): It removes whitespaces from the beginning and end of the
string.
String Methods
• isdigit(): Returns True if all characters in a string are digits.
• isalpha(): Returns True if all characters in a string are characters from
an alphabet.
• isspace(): Returns True if all characters in a string are spaces.
• startswith(): It takes a string as an argument, and returns True is the
string it is applied on begins with the string in the argument.
String Methods
• endswith(): It takes a string as an argument, and returns True if the
string it is applied on ends with the string in the argument.
• find(): It takes an argument and searches for it in the string on which
it is applied. It then returns the index of the substring
• If the string doesn’t exist in the main string, then the index it returns
is 1..
String Methods
• split(): It takes one argument. The string is then split around every
occurrence of the argument in the string.
• replace(): It takes two arguments. The first is the substring to be
replaced. The second is the substring to replace with.
• join(): It takes a list as an argument and joins the elements in the list
using the string it is applied on.
String Functions
• len(): The len() function returns the length of a string.
• str(): This function converts any data type into a string.
Methods
• lower() and upper(): These methods return the string in lowercase
and uppercase, respectively.
• strip(): It removes whitespaces from the beginning and end of the
string.
String Methods
• isdigit(): Returns True if all characters in a string are digits.
• isalpha(): Returns True if all characters in a string are characters from
an alphabet.
• isspace(): Returns True if all characters in a string are spaces.
• startswith(): It takes a string as an argument, and returns True is the
string it is applied on begins with the string in the argument.
String Methods
• endswith(): It takes a string as an argument, and returns True if the
string it is applied on ends with the string in the argument.
• find(): It takes an argument and searches for it in the string on which
it is applied. It then returns the index of the substring
• If the string doesn’t exist in the main string, then the index it returns
is 1..
String Methods
• split(): It takes one argument. The string is then split around every
occurrence of the argument in the string.
• replace(): It takes two arguments. The first is the substring to be
replaced. The second is the substring to replace with.
• join(): It takes a list as an argument and joins the elements in the list
using the string it is applied on.
Comparison
• ‘hey’ is lesser than ‘hi lexicographically
>>> a=‘hey
>>> b=‘hi’
>>> a<b
True
Arithmetic
>>> print(“Ba”+”ta”*2)
??
Tuples
• A tuple is a collection of items which are ordered and immutable.
• Tuples are sequences, just like lists.
• The differences between tuples and lists are, the tuples cannot be
changed unlike lists
• Tuples use parentheses, whereas lists use square brackets.
Creating Tuple
• tup1 = ('physics', 'chemistry', 1997, 2000)
• tup2 = (1, 2, 3, 4, 5 )
• T1 = ()
• Tuples are immutable which means you cannot update or change the
values of tuple elements.
PACKING
a="a","b","c","d“
print(a)
UNPACKING
p,q,r,s = a
print(q)
??
Nested tuples
b=((4,5,6),(8,9,1))
>>> b[0]
(4, 5, 6)
>>> b[0][2]
6
Deleting elements
• Removing individual tuple elements is not possible.
t =(4,5,6,7)
del t[1] will give error
dict2={1:2,1:3,1:4,2:4}
print(dict2)
{1: 4, 2: 4}
Accessing
D3={0: 0, 1: 1, 4: 2, 9: 3, 16: 4, 25: 5, 36: 6, 49: 7}
>>> D3[36]
???
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
>>> dict[‘Age’]
Functions
• len()
• sorted()
Methods
• keys()
• values()
• items()
• get()
• clear()
• copy()
• pop()
• popitems()
• fromkeys()
Nested Dictionary
>>> dict1={4:{1:2,2:4},8:16}
>>> dict1
{4: {1: 2, 2: 4}, 8: 16}