Professional Documents
Culture Documents
UM20MB603 - Python - Unit 2
UM20MB603 - Python - Unit 2
Lekha A
Computer Applications
PYTHON PROGRAMMING
Lekha A
Computer Applications
PYTHON PROGRAMMING
Why to use Built-in Types
• For simple tasks, built-in types are often all that is needed
structures.
(e.g., C++).
same.
PYTHON PROGRAMMING
Literals
• Integer
• Float
• Complex
PYTHON PROGRAMMING
Numeric Literals
PYTHON PROGRAMMING
String Literals
quotes.
double quotes.
PYTHON PROGRAMMING
String Literals
PYTHON PROGRAMMING
Boolean Literals
• True
• False
PYTHON PROGRAMMING
Special Literals
• Numbers
• List
• Tuple
• Strings
• Set
• Dictionary
PYTHON PROGRAMMING
Data types - Numbers
• Integers – int
memory available.
PYTHON PROGRAMMING
Data types - Numbers
places.
strings.
subscripted as well.
by commas.
PYTHON PROGRAMMING
Data types - Sets
flexible.
].
PYTHON PROGRAMMING
Data types - Dictionary
indexed.
• type(a)
PYTHON PROGRAMMING
Examples of built-in functions
• id(a)
PYTHON PROGRAMMING
Examples of built-in functions
• a.bit_length()
PYTHON PROGRAMMING
Examples of built-in functions
as_integer_ratio()
PYTHON PROGRAMMING
Conversion or Casting
functions:
• int()
• float()
• str().
PYTHON PROGRAMMING
Conversion or Casting
• int()
• An integer literal
number) literal
number)
PYTHON PROGRAMMING
Conversion or Casting
PYTHON PROGRAMMING
Conversion or Casting
• float()
• An integer literal
• A float literal
or an integer)
PYTHON PROGRAMMING
Conversion or Casting
PYTHON PROGRAMMING
Conversion or Casting
• str()
including
• Strings
• Integer literals
• Float literals
PYTHON PROGRAMMING
Conversion or Casting
PYTHON PROGRAMMING
Conversion or Casting
• List()
• Tuple()
• Set()
• Dict()
Python interpreter.
user.
starts
given indices.
PYTHON PROGRAMMING
Working with Strings
PYTHON PROGRAMMING
Working with Strings
• str1+str2
PYTHON PROGRAMMING
Working with Strings
• str1 * 3
PYTHON PROGRAMMING
Working with Strings
• string.strip()
PYTHON PROGRAMMING
Working with Strings
• len(string)
PYTHON PROGRAMMING
Working with Strings
• string.lower() or string.upper()
PYTHON PROGRAMMING
Working with Strings
replaces”)
PYTHON PROGRAMMING
Working with Strings
• string.split(“sep")
PYTHON PROGRAMMING
Working with Strings
• string.capitalize()
PYTHON PROGRAMMING
Working with Strings
a string
• string.count(“Pattern”)
PYTHON PROGRAMMING
Working with Strings
• find() searches the string for a specified value and returns the
• string.find(“pattern”)
PYTHON PROGRAMMING
Working with Strings
case
case
a list.
mental checklists.
PYTHON PROGRAMMING
Lists
• Our DNA is essentially a long list of molecules in the form This Photo by Unknown Author is
licensed under CC BY-ND
computational problem.
PYTHON PROGRAMMING
Lists
PYTHON PROGRAMMING
Lists
altered.
• Thus, all lists have index values 0..n-1, where n is the number
a linear ordering.
given week:
on.
PYTHON PROGRAMMING
Lists
rather than 1.
element of a list.
• Access (retrieve)
• Update
• Append
• Insert
• Delete (remove)
PYTHON PROGRAMMING
Lists
PYTHON PROGRAMMING
Lists - Operations
[].
PYTHON PROGRAMMING
Lists - Operations
square brackets,
• list([iterable])
iterator object
PYTHON PROGRAMMING
Lists - Creation
of iterable's items.
PYTHON PROGRAMMING
Lists - Operations
square brackets,
• To update, lst[2] = 4
• To insert, lst.insert(1,3)
• To append, lst.append(4)
• e.g.,
• list2 = list1
in memory.
PYTHON PROGRAMMING
Assignment of Lists
• This issue does not apply to strings and tuples, since they are
• list3 = list(list1)
the other.
PYTHON PROGRAMMING
Slice of Lists
stop, step).
PYTHON PROGRAMMING
Slice of Lists - Output
5, 6, 7, 8]
• print(n[-4:-1])
• print(n[-1:-4])
• print(n[-5:])
• print(n[-6:-2:2])
• print(n[::-1])
PYTHON PROGRAMMING
Tuples
altered.
• Var=tuple((iterables))
as shown below,
• CORRECT WRONG
• (1) 1
PYTHON PROGRAMMING
Tuples
defined on tuples.
PYTHON PROGRAMMING
Sets
following attributes.
to an integer.
• Set is an iterable
a tuple or a string.
PYTHON PROGRAMMING
Sets
• Membership : in
• Union : |
• Intersection : &
• Set difference : -
• Symmetric difference : ^
PYTHON PROGRAMMING
Sets
• Set constructor { … }
altered.
PYTHON PROGRAMMING
Sequence
PYTHON PROGRAMMING
Nested Lists and Tuples
other sequences.
• To access the first exam grade of the first student in the list,
• student1_grades = class_grades[0]
• student1_exam1 = student1_grades[0]
85
• OR
• class_grades[0][0] 8
PYTHON PROGRAMMING
Dictionary Methods
value pairs
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators
PYTHON PROGRAMMING
Arithmetic Operators
• + Addition x+y
• - Subtraction x-y
• * Multiplication x * y
• / Division x/y
• % Modulus x%y
• ** Exponentiation x ** y
• // Floor division x // y
PYTHON PROGRAMMING
Assignment Operators
= 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
• > Greater that - True if left operand is greater than the right
• < Less that - True if left operand is less than the right
binary digits.
• They are used to check if two values (or variables) are located
• Two variables that are equal does not imply that they are
identical.
PYTHON PROGRAMMING
Identity Operators
object) x is True
• is not True if the operands are not identical (do not refer to
sequence.
5 in x
5 not in x
PYTHON PROGRAMMING
Accessing through index
to y position.
PYTHON PROGRAMMING
Accessing through index
onwards.
• The slice() function returns a slice object that can use used to
starts
string val2,…)
times.
PYTHON PROGRAMMING
List Comprehensions
of code.
• l1 = []
• for x in range(5):
• l1.append(x)
PYTHON PROGRAMMING
List Comprehensions
disk.
a filename.
of files.
• The text and binary files has .txt and .bin as their
extensions respectively.
PYTHON PROGRAMMING
Introduction
approaches.
new file.
• The open() function will create the file with the specified
• The access mode is the string which tells in what mode the file
• Reading
• Writing
existing file.
PYTHON PROGRAMMING
Creating a file
• Append
• Reading Mode
• Writing Mode
• Append Mode
•
PYTHON PROGRAMMING
Creating a new file
• file.name
• file.mode
• file.closed
•
PYTHON PROGRAMMING
Creating a new file
• file_object.write(“String”)
closed.
PYTHON PROGRAMMING
Reading from a file
• The text information in the text file can be extracted with the
• var_name = file_object.read(index)
•
PYTHON PROGRAMMING
Closing a file
and closed.
files in python.
• file_object.close()
PYTHON PROGRAMMING
Closing a file
•
PYTHON PROGRAMMING
Appending to a file
Method Description
Method Description
not
Method Description
Method Description
file position
•
PYTHON PROGRAMMING
Renaming a file
• os.rename(old_name, new_name)
PYTHON PROGRAMMING
Renaming a file
•
PYTHON PROGRAMMING
Renaming a file
error.
PYTHON PROGRAMMING
Renaming a file
•
PYTHON PROGRAMMING
Deleting a file
• os.remove(file_name)
PYTHON PROGRAMMING
Deleting a file
PYTHON PROGRAMMING
Deleting a file
•
THANK YOU
Lekha A
Department of Computer Applications
lekha@pes.edu
+91 80 6666 3333 Extn 899