Subscribe this Blog

Wikipedia

Search results

Notifications and New Posts!

Tuesday, November 24, 2020

YouTube Premiered Lecture - November 26, 2020, 03:30 PM - ADVANCED PROGRAMMING CONCEPTS - Python (TY A and B)

 ADVANCED PROGRAMMING CONCEPTS - Python

Due to internet/network connectivity issue, 

I will be conducting lecture on YouTube. 
Lecture will start at sharp 03:30 PM.
Please attend it. And if you have any questions then
please ask in Comment box below the video.

Topics which will be covered in this session are: 1. Python String Formatting 2. Python File Handling

Due to Internet connectivity issue making premiered video can be great tool 
where without any kind of interruption students can watch lecture video.

YouTube Session
ADVANCED PROGRAMMING CONCEPTS - Python (TY A and B)
Punyashlok Ahilyadevi Holkar Solapur University.
TY Mechanical Engineering Students.
Ref.: https://www.w3schools.com/python/
For more clarity please watch YouTube video with 360p or 480p Quality. 
Please click on the three vertical dots on video then select Quality option.
Then select resolution as 360p or 480p.

As per Dean Students, all students are requested to attend the following session first.
DISHA BHARAT presents National Webinar on National Education Policy (NEP) 2020, VIKASANA NEP - 2020: VISION TO ACTION on November 26, 2020 at 10.30 AM – 1.30 PM
Date: November 26, 2020 Time: 10.30 AM – 1.30 PM Via: Zoom Link    
The event will be LIVE on www.facebook.com/DishaBharat

Online classes will be conducted after end of the above mentioned session with one hr lunch break. Therefore Re-scheduled our python session at 03:30 PM.
Please note it and act accordingly.

Please write you Name and Roll Number in Live Chat Box/Comment Box (below Video in YouTube) to capture your attendance.
YouTube Video Lecture: 
This video is premiered it will start at sharp 03:30 PM.
To ask questions in Chat Box/Comment Box :





Facebook Event Registration : Click here

Python Programs:

# Python String Formatting
# To make sure a string will display as expected,
# we can format the result with the format() method.
#
# String format()
# The format() method allows you to format selected parts of a string.
#
# Sometimes there are parts of a text that you do not control,
# maybe they come from a database, or user input?
#
# To control such values, add placeholders (curly brackets {}) in the text,
# and run the values through the format() method:


price = 49
#txt = "The price is {} Rupees"
string1="The price is {} Rupees"
print(string1.format(price))

Output:
The price is 49 Rupees

# You can add parameters inside the curly brackets to specify
# how to convert the value:
#
# Example
# Format the price to be displayed as a number with two decimals:

#price = 49.5607
price = 49.5677

txt = "The price is {:.1f} Rupees"
print(txt.format(price))

txt = "The price is {:.2f} Rupees"
print(txt.format(price))
Output:
The price is 49.6 Rupees
The price is 49.57 Rupees

# Multiple Values
# If you want to use more values, just add more values to the format() method:
quantity = 3
itemno = 567
price = 49
myorder = "I want {} pieces of item number {} for {:.2f} Rupees"
print(myorder.format(quantity, itemno, price))
#print(myorder.format(itemno,quantity, price))

Output:
I want 3 pieces of item number 567 for 49.00 Rupees


# Index Numbers
# You can use index numbers (a number inside the curly brackets {0})
# to be sure the values are placed in the correct placeholders:


quantity = 3
itemno = 567
price = 49
myorder = "I want {0} pieces of item number {1} for {2:.2f} Rupees"
print(myorder.format(quantity, itemno, price))
Output:
I want 3 pieces of item number 567 for 49.00 Rupees



# age = 21
# name = "Rahul"
# txt = "His name is {1}. {1} is {0} years old."
# print(txt.format(age, name))


age = 21
name = "Rahul"
txt = "His name is {0}. {0} is {1} years old."
print(txt.format(name,age))

Output:
His name is Rahul. Rahul is 21 years old.



# Named Indexes
# You can also use named indexes by entering
# a name inside the curly brackets {carname},
# but then you must use names when
# you pass the parameter values txt.format(carname = "Ford"):
#
# Example
myorder = "Rahul have a {carname} car. He has a {model} model. Rahul's age is {age}"
print(myorder.format(carname = "Maruti", model = "K10", age = 21))
Output:
Rahul have a Maruti car. He has a K10 model. Rahul's age is 21





# Python File Open
# File handling is an important part of any web application.
# Python has several functions for creating, reading, updating, and deleting files.

# File Handling
# The key function for working with files in Python is the open() function.
#
# The open() function takes two parameters; filename, and mode.
#
# There are four different methods (modes) for opening a file:
#
# "r" - Read - Default value. Opens a file for reading,
#     error if the file does not exist
#
# "a" - Append - Opens a file for appending,
#     creates the file if it does not exist
#
# "w" - Write - Opens a file for writing,
#     creates the file if it does not exist
#
# "x" - Create - Creates the specified file,
# returns an error if the file exists

# In addition you can specify if the file should be handled as binary or text mode
#
# "t" - Text - Default value. Text mode
#
# "b" - Binary - Binary mode (e.g. images)

f = open("demofile6.txt","x")

#f = open("demofile.txt", "rt")

#Note: Make sure the file exists, or else you will get an error.

# To open the file, use the built-in open() function.
#
# The open() function returns a file object,
# which has a read() method for reading the content of the file:

f = open("demofile.txt", "r")
print(f.read())
Output:
Hello! Welcome to demofile.txt yes we are in first line
This file is for testing purposes.
Good Luck!
Welcome to Mechanical Engineering!!

demofile.txt file

Hello! Welcome to demofile.txt yes we are in first line
This file is for testing purposes.
Good Luck!
Welcome to Mechanical Engineering!!



f = open("F:\myfile\welcome.txt", "r")
print(f.read())
Output:
We are in welcome.txt file!!!



F:\myfile\welcome.txt

We are in welcome.txt file!!!

Python Programs:
# Read Only Parts of the File
# By default the read() method returns the whole text,
# but you can also specify how many characters you want to return:
#
# Example
# Return the 5 first characters of the file:

f = open("demofile.txt", "r")
#print(f.read(6))
print(f.read(5))
Output:
Hello



# Read Lines
# You can return one line by using the readline() method:
#
# Example
# Read one line of the file:

f = open("demofile.txt", "r")
print(f.readline())
print(f.readline())
Output:
Hello! Welcome to demofile.txt yes we are in first line

This file is for testing purposes.



# By calling readline() two times, you can read the two first lines:
#
# Example
# Read two lines of the file:

f = open("demofile.txt", "r")
print(f.readline())
print(f.readline())
print(f.readline())
Output:
Hello! Welcome to demofile.txt yes we are in first line

This file is for testing purposes.

Good Luck!

# Example
# Loop through the file line by line:

f = open("demofile.txt", "r")
for x in f:
  print(x)
Output:
Hello! Welcome to demofile.txt yes we are in first line

This file is for testing purposes.

Good Luck!

Welcome to Mechanical Engineering!!



# Close Files
# It is a good practice to always close the file when you are done with it.
#
# Example
# Close the file when you are finish with it:

f = open("demofile.txt", "r")
print(f.readline())
f.close()

# Note: You should always close your files,
# in some cases, due to buffering,
# changes made to a file may not show until you close the file.
Output:
Hello! Welcome to demofile.txt yes we are in first line



# Write to an Existing File
# To write to an existing file, you must add a parameter to the open() function:
#
# "a" - Append - will append to the end of the file
#
# "w" - Write - will overwrite any existing content

# Example
# Open the file "demofile2.txt" and append content to the file:

f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()

#open and read the file after the appending:
f = open("demofile2.txt", "r")
print(f.read())
Output:
Now the file has more content!



# Example
# Open the file "demofile3.txt" and overwrite the content:

f = open("demofile2.txt", "w")
f.write("Woops! I have deleted the content!")
f.close()

#open and read the file after the appending:
f = open("demofile2.txt", "r")
print(f.read())

#Note: the "w" method will overwrite the entire file.
Output:
Woops! I have deleted the content!



# Create a New File
# To create a new file in Python, use the open() method,
# with one of the following parameters:
#
# "x" - Create - will create a file, returns an error if the file exist
#
# "a" - Append - will create a file if the specified file does not exist
#
# "w" - Write - will create a file if the specified file does not exist

# Example
# Create a file called "myfile.txt":

f = open("myfile1.txt", "x")

#Result: a new empty file is created!

Output:




#Create a new file if it does not exist:

f = open("myfile.txt", "w")

Output:




# Python Delete File
# Delete a File
# To delete a file, you must import the OS module,
# and run its os.remove() function:
#
# Example
# Remove the file "demofile.txt":

import os
os.remove("demofile2.txt")

Output:



# Check if File exist:
# To avoid getting an error,
# you might want to check if
# the file exists before you try to delete it:
#
# Example
# Check if file exists, then delete it:

import os
if os.path.exists("myfile.txt"):
  os.remove("myfile.txt")
else:
  print("The file does not exist")

#os.remove("myfile.txt")
Output:




# Check if File exist:
# To avoid getting an error,
# you might want to check if
# the file exists before you try to delete it:
#
# Example
# Check if file exists, then delete it:

import os
if os.path.exists("myfile.txt"):
  os.remove("myfile.txt")
else:
  print("The file does not exist")

#os.remove("myfile.txt")
Output:
The file does not exist



# Delete Folder
# To delete an entire folder, use the os.rmdir() method:
#
# Example
# Remove the folder "myfolder":

import os
os.remove("myfolder\myfile.txt")
os.rmdir("myfolder")
#Note: You can only remove empty folders.
Output:
Traceback (most recent call last):
  File "C:/Users/Hp/PycharmProjects/Python_File_Handling/15.py", line 8, in <module>
    os.remove("myfolder\myfile.txt")
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'myfolder\\myfile.txt'



Present Students:
TYB_34 Madan Patil ​TYB_34
Rohit Salgar ​TY B 74
TYB_44 Aftab Shaikh ​TYB_44
TYB_41 Yasar Khatik ​TYB_41
ABHI KHOTE ​TYB_22
TYB_33 Ridham Parmar ​TYB_33
Vaishnavi Lakheri ​TYA_04
manthan dixit ​TYB16
saurabh chavan ​TYA_16
TYA_73 Ashley Thomas ​TYA_73
TB48 Vishwajit shinde ​TYB_48
SAMARTH CHAVAN ​TA 15
Suraj More ​TYA 45
TYA_44 Mangesh Misal ​TYA_44
Prajwal Musale ​TYA_55
TYA_29 Yuvraj Shelar ​TYA_29
ADESH BANKAR ​TYB_09
Aditya Motewar ​TYA46
Harshal Nagtilak ​TYB_30
Omkar Jagtap ​TYB_66
Milind Jagadale ​TA34
Shraddha gajakosh ​TYA_02
Yash Gadekar ​TYA_25
TYA_05 Saleha Mirjkar ​TYA-05
Gayatri Joshi ​TYA_03
TYA_13 Atish Jadhav ​TYA_13
TYB_03 Arati Lale ​TYB 03
Harshvardhan Ubale ​TYB_50
Irfan Mulani ​TYA_54
Vijay Mali ​TYB_26
TYB_41 Yasar Khatik ​TYB_41
Rutik Godse ​TYA_31
Pranay Disale ​TYA_50
Sachin Waghmare ​TA49
bharat burungale ​TA14
TYB_54 Rohit Vidhate ​TYB54
Shashikant Thorat ​TYA_66
Om Metkari ​TYB-28
AMIT YELE ​TYB-76
sumit Patwari ​TYB_77
Sushant Jadhav ​TYB_65
Jaydev nanaware ​TYB_31
Dhanashree Sonawane ​TYB06
Ganesh Todkar ​TA24
Kiran Kasulwar ​TYA-20
Samart h Mane-Deshmukh ​TYA_51
TYB_55 Saurabh Wadekar ​TYB 55
Deepak Shinde ​TYB 46
Pravin Somdale ​TYA_65
Atharv Kulkarni ​TYB_23
Nikhil Mundhe ​TYA47
Shubham Deokar ​TYB72
TYB_71_Onkar Kale ​TY_B_71
aniket bansode ​TYB_10
TYB_58 SANDIP BAGUL ​TYB_58
deepjyoti sathe ​TA08
VINAYAK Gawali TYA_72
Namrata Parvat TYA_07
Hritik Bhosale ​TYB 12
TYB_41 Yasar Khatik ​TB 41
OMKAR PAWAR ​TYB37
Nilesh Kadam ​TYB-21
Santosh Patil ​TYB_35
Kashiling Sarak ​TYB_42
Mohit Taur ​TYB_51
TYB_75 Hrushikesh Walujkar ​TYB_75
TYB_41 Yasar Khatik ​TYB_41
Sachin Kshirsagar ​TYA_42
Vaibhav Bhosale ​TB13
TYA_71 Pavan Gavali ​TYA_71
Akshay Pansare ​TA43