0% found this document useful (0 votes)
36 views

Python Assignment

Python has several key features including being easy to learn, portable across platforms, supporting both procedural and object-oriented programming, being dynamically typed and extensible. Global variables can be accessed anywhere in a program while local variables are only accessible within their defined scope like a function. String formatting replaces placeholders with values while string replacement returns a copy of the string with all occurrences of a substring replaced. CSV files can have different formats that affect character encoding while text files use line endings and binary files store data as 0s and 1s without line endings.

Uploaded by

Himanshu Bundel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Python Assignment

Python has several key features including being easy to learn, portable across platforms, supporting both procedural and object-oriented programming, being dynamically typed and extensible. Global variables can be accessed anywhere in a program while local variables are only accessible within their defined scope like a function. String formatting replaces placeholders with values while string replacement returns a copy of the string with all occurrences of a substring replaced. CSV files can have different formats that affect character encoding while text files use line endings and binary files store data as 0s and 1s without line endings.

Uploaded by

Himanshu Bundel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Himanshu Bundel

20210213060235

Assignment for backlog in Python Programming

Q.1. What are key features of Python?


1: Python is actually very easy-to-learn - Python programming language gained its popularity just
because of less usage of keywords, simple structure and clearly defined syntax. People picked python
because the codes are easily readable and understandable. Due to such shift many online python
courses also made their way and people found an easier way to learn the language.
2: Python can be ported to other platforms- Python is an open source language i.e. it can be ported to
different platforms. Python has no compatibility issues with other platforms unless all the system
dependent features are fixed. Like Perl, stock Python even offers a portable set of bindings to the Tk
toolkit that supports portable GUIs across Unix, MacOS, and Windows.
3: Object- Oriented Language - Great thing about Python is that it has support for both procedure-
oriented programming as well as object-oriented programming. The programs for procedure-oriented
languages are built around functions or procedures and can be reused. In object-oriented languages,
the program is built around objects which combine data and functionality. An object-oriented
language includes data and functionality in their object. Unlikely many big languages like C++ or
Java, Python comes with a very powerful and simple way of using object-oriented programming.  
4:  Dynamic in Nature  - Python owns this dynamic nature which makes the execution very easy. The
modules are compiled at import time with the interpretation of object messages done and problem
reported at the run time.                             
5:  Extendable & Scalable - Python can be extended by adding low-level modules to its interpreter.
The modules only enable the programmers to add or modify their tools to be more efficient. Python is
also scalable because it provides a better structure and support for large programs than shell scripting.

Q.2. What are Global & local variables in pyhton?

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

3. What are Python String formats and Python String replacements?

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. 

Replace All Instances of a Single Character using replace()

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.

4. Discuss the different formats of .CSV & Text files in python.

our CSV-related formats available, as follows:

 CSV UTF-8 (Comma delimited)


 CSV (Comma delimited)
 CSV (Macintosh)
 CSV (MS-DOS)

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.

You might also like