Module 6 Python
Module 6 Python
Example:
x = 1 # inty = 2.8 # floatz = 1j # complex
x = 1y = 35656222554887711z = -3255522print(type(x))print(type(y))print(type(z))
Mathematical Functions
The min() and max() functions can be used to find the lowest or highest value in an iterable:
Mathematical Functions
The math.fmod() method returns the remainder (modulo) of x/y.
import math# Return the remainder of x/y
print(math.fmod(20, 4))
print(math.fmod(20, 3))
0
2
import math
print(math.pow(2, 3)) #8
import math
print (math.sqrt(9)) #3
print (math.sqrt(25)) #5
import math
T= (1, 2, 3)
L = (3, 4, 5)
#Return the product of the elements
print(math.prod(T)) #6
print(math.prod(L)) #60
apple
Example:
str1 = "Hello, world!"
str2 = "I love Python."
str3 = "Hello, world!"
str="Python"
for i in str: P
print(i) y
t
h
o
n
name= "Havya"
count=0
for char in name:
if char== 'a':
count=count+1
print("No of a in my name is:",count)
No of a in my name is: 2
vowels="aeiou"
my_str = "Hello I am Devansh"
no_vowel = ""
for char in my_str:
if char not in vowels:
no_vowel= no_vowel + char
print(no_vowel)
Hll I m Dvnsh
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
my_str = "Hello!!!, he said ---and went."
no_punct = ""
for char in my_str:
if char not in punctuations:
no_punct = no_punct + char
print(no_punct)
Output:
Hello he said and went
O/P: True
index():Searches the string for a specified value and returns the position of first occurrence where it was found
The index() method returns exception if the value is not found.
Ex1:
txt = "welcome to the Python Class"
x = txt.split()
print(x) O/P: ['welcome', 'to', 'the', 'Python', ‘Class']
Ex2:
Version-1:
ori_str = "hi this is a python class"
words= ori_str.split()
words.sort()
sor_str=" ".join(words)
print("Orginal:",ori_str)
print("After Sorting:",sor_str) Orginal: hi this is a python class
After Sorting: a class hi is python this
Types of files
file attributes:
1. Name: File name is the name given to the file. A name is usually a string of characters.
2. Identifier: Identifier is a unique number for a file. It identifies files within the file system. It is
not readable to us, unlike file names.
3. Type: Type is another attribute of a file which specifies the type of file such as archive file
(.zip), source code file (.c, .java), .docx file, .txt file, etc.
4. Location: Specifies the location of the file on the device (The directory path). This attribute is
a pointer to a device.
5. Size: Specifies the current size of the file (in Kb, Mb, Gb, etc.) and possibly the maximum
allowed size of the file.
6. Protection: Specifies information about Access control (Permissions about Who can read,
edit, write, and execute the file.) It provides security to sensitive and private information.
7. Time, date, and user identification: This information tells us about the date and
time on which the file was created, last modified, created and modified by which user, etc.
File I/O-Opening and Closing files
In Python, we use the open() method to open files.
open() returns a “file handle/pointer” is a variable used to perform operations
on the file.
A file handle or pointer denotes the position from which the file contents will be
read or written.
When file is opened, file pointer points to the beginning of the file.
Example:
f = open("demofile.txt", "r")
File I/O-Opening and Closing files
File I/O-Opening and Closing files
Syntax : filehandle.write(string);
Here, string parameter is the content to be written into the opened
file.
f = open('first.txt' , 'w')
str = "Hello Welcome to Python class"
f.write(str) first.txt
Hello Welcome to Python class
f.close()
f = open('first.txt' , 'a')
str = " File Handling"
f.write(str)
f.close() first.txt
Hello Welcome to Python class File Handling
N=2
f = open( 'crickter.txt' , 'r') O/P:
print(f.readlines()[N:]) ['Ganguly\n', 'Laxman']
f.close()
f.seek(5)
str2 = f.read()
print("After Seek:",str2)
f.seek(0,2)
Before: Hello this is VIT-AP University
str3 = f.read() After Seek: this is VIT-AP University
print("AFter End:",str3) AFter End:
f.close()
Ex:
import os
os.rename("one.txt","two.txt")
os.remove("three.txt")
f1=open("crickter.txt","r")
f2=open("cri.txt","a")
line=f1.read()
f2.write(line)
CSV (Comma-Separated Values) is one of the file formats for storing and
exchanging tabular data.
import csv
f=open('protagonist.csv', 'r’)
reader = csv.reader(f)
for row in reader: O/P:
print(row) ['SN', 'Movie', 'Protagonist']
['1', 'Lord of the Rings', 'Frodo Baggins']
['2', 'Harry Potter', 'Harry Potter']
import csv
f=open('protagonist.csv', 'w’)
writer = csv.writer(f)
writer.writerow(["SN", "Movie", "Protagonist"])
writer.writerow([1, "Lord of the Rings", "Frodo Baggins"])
writer.writerow([2, "Harry Potter", "Harry Potter"])
Creating a Directory
import os
os.mkdir('mydir’)
Change Directory
import os
os.chdir('mydir’)
Renaming a Directory
import os
os.rename('mydir','mydir2’)
Deleting a Directory
import os
os.rmdir('mydir’)