Python Assignment
Python Assignment
20210213060235
Local variables can only be reached within their scope (like func( above)
Like in below program- there are two local variables - x and y.
def sum (x, y) :
sum = X + y
return sum
print (sum (5, 10))
Output
15
The variables x and y will only work/used inside the function sum( and they don't exist outside
of the function. So trying to use local variable outside their scope, might through NameError.
So obviously below line will not work.
print (x)
A global variable can be used anywhere in the program as its scope is the entire program
Let's understand global variable with a very simple example
z = 25
def func() :
global z
print(z)
2=20
func()
print(z)
Output
25
20
A calling func(), the global variable value is changed for the entire program.
Below example shows a combination of local and global variables and function parameters
def func(x, y) :
global a
a
= 45
X, y = y, x
b = 33
b = 17
C= 100
print (a.b.x.v)
: a,b, X, y = 3,15,3,4
func (9,81)
print (a, b,x, y)
En Route Jewel
for Select One:
En Route Jewelry
Output
45 17 81 9
45 15 3 4
Python String format() is a function used to replace, substitute, or convert the string with
placeholders with valid values in the final string. It is a built-in function of the Python string class,
which returns the formatted string as an output. The placeholders inside the string are defined in curly
brackets.
For example, “Welcome to Guru99 {}”.format(‘value here’).
The replace() in Python returns a copy of the string where all occurrences of a substring are
replaced with another substring.
Syntax of replace()
Syntax: string.replace(old, new, count)
Parameters:
old – old substring you want to replace.
new – new substring which would replace the old substring.
count – (Optional ) the number of times you want to replace the old substring with the
new substring.
Return Value : It returns a copy of the string where all occurrences of a substring are replaced with
another substring.
In this example, we are only replacing a single character from a given string. The Python replace()
method is case-sensitive, and therefore it performs a case-sensitive substring substitution, i.e. R in
FOR is unchanged.
Each format affects character encoding in slightly different ways. For example, the Macintosh format
uses a CR (carriage return) as the terminating character for a record or a line, while Windows based
formats—in essence, the other three—use CR/LF (carriage return/line feed). So, each format is
slightly different.
The difference between the three formats is based on which code page is used with each format. Code
pages have to do with the way in which individual characters are encoded, and it typically comes into
play if you use extended characters—such as foreign characters or accented characters—in your data.
The code pages used by each format can vary, depending on (1) the version of Excel you are using,
(2) which language version of Excel you are using, and (3) how your regional settings are configured.
In other words, there is no fast-and-hard rule about what code pages will be used with which CSV
format you choose for your export.
There are two types of files that can be handled in python, normal text files and binary files (written
in binary language, 0s, and 1s).
Text files: In this type of file, Each line of text is terminated with a special character
called EOL (End of Line), which is the new line character (‘\n’) in python by default.
Binary files: In this type of file, there is no terminator for a line, and the data is stored
after converting it into machine-understandable binary language.
Access modes govern the type of operations possible in the opened file. It refers to how the file will
be used once its opened. These modes also define the location of the File Handle in the file. File
handle is like a cursor, which defines from where the data has to be read or written in the file. There
are 6 access modes in python.
1. Read Only (‘r’) : Open text file for reading. The handle is positioned at the beginning
of the file. If the file does not exists, raises the I/O error. This is also the default mode
in which a file is opened.
2. Read and Write (‘r+’): Open the file for reading and writing. The handle is positioned
at the beginning of the file. Raises I/O error if the file does not exist.
3. Write Only (‘w’) : Open the file for writing. For the existing files, the data is truncated
and over-written. The handle is positioned at the beginning of the file. Creates the file if
the file does not exist.
4. Write and Read (‘w+’) : Open the file for reading and writing. For an existing file,
data is truncated and over-written. The handle is positioned at the beginning of the file.
5. Append Only (‘a’): Open the file for writing. The file is created if it does not exist.
The handle is positioned at the end of the file. The data being written will be inserted at
the end, after the existing data.
6. Append and Read (‘a+’) : Open the file for reading and writing. The file is created if it
does not exist. The handle is positioned at the end of the file. The data being written
will be inserted at the end, after the existing data.