Beruflich Dokumente
Kultur Dokumente
UNIT2
PART – A
Keyword are also reserved words that has particular meaning and usage in the language.
User cannot edit the meaning or usage of the keywords he can only able to use them.
Arithmetic Operators
Comparison Operators
Logical Operators
Assignment Operators
Bitwise Operator
Conditional Operators or ternary operator
Membership operators
Lambda operator
Identity operator
You don’t have to repeatedly type the same value if its used multiple times in program
If you want to change the value from 3.14 to 3.14159 then we need to change in only one
place. Its very useful & time saving.
Descriptive names makes the program more easy to read.
Data types
Int
List
Dictionary
Float
Tuple
Complex
String
Bool
Mutable objects:
These are the objects whose values can be able to change at any time during program
execution after it assigned to variable.
Examples: list, tuples
Immutable Objects:
These are the objects whose values cannot be able to change at any time during program
execution after it assigned to variable.
Example: string,dictionary
Function is a set of instruction that are used to perform some task (or) process which
frequently occur in main program.
Functions is a sub-program that contains one or more statements and it performs some task
when called.
Types:
Actual parameters:
When a function is invoked, you pass a value to the parameter. This value is referred to as
actual parameter or argument.
In the above example: x, y are actual parameters
Formal parameters:
The variables defined in the function header are known as formal parameters.
In the above example: num1, num2 are formal parameters
11. Write a program to calculate area & perimeter of circle using functions.
12. Write a program to swap the content of two variables with using third variable or without
using third variable
14. Write a program for simple calculator perform add, sub, mul, product.
Def: The smallest individual unit element in the program is known as token or lexical unit.
1. Keywords 4. Operators
2. Identifiers 5. Punctuators
17. What is the difference between pure arithmetic and mixed arithmetic expression?
Pure arithmetic expression which has value of pure integer or floating point or complex
numbers
Mixed expression which is combination of all integer, float, and complex numbers.
PART – B
UNIT 3
PART A
CONTROL STRUCTURES
CONTINUE
IF FOR
IF ELSE PASS
WHILE
IF ELIF ELSE
BREAK CONTINUE
It takes the control to the outside of the It takes the control to the beginning of the
loop loop
Keyword: break Keyword: continue
Mainly used inside loops Mainly used inside loops
1. if
2. ifelse
3. ifelif..else
1. for
2. while
3. for / while with else
Slicing operation is used to return / select / slice the particular substring based on
user requirements.
A segment of the string is called slice.
Syntax: string_variable_name [start:end:step value]
Here all the 3 parameters / variables are optional. i.e user may or may not mention
The default value for start is zero
The default value for end is -1(minus 1)
The default value for step is +! Or simply 1(one)
End value is taken as end_Value = end_Value -1
Because as we said start value index is zero(0) and start from there print 4 word
continuously. So only “Welc”
We know that string is immutable in nature. That is we cannot edit the string value
once its assigned to variable.
If we try to assign or replace a value in the stringits show error that str is not
mutable object.
String functions and method contain all above seen string functions such as:
1. capitalize 8. isupper()
2. find 9. string.lower()
3. isalnum() 10. string.upper()
4. isalpha() 11. string.lstrip()
5. isdigit() 12. string.rstrip()
6. isspace() 13. string.strip()
7. islower()
Python functions is said to be fruitful function if it return any value to the calling function.
A function that don’t return any value is called void function.
For While
Syntax: Syntax:
for var_name in While(condition):
range(start:end:stepvalue): #body
#for body
It is count controlled loop It is condition control loop
It is entry controlled loop It is entry controlled loop
13. Explain while loop with else block and its use
There is a structural similarity between while and else statement. Both have a block of
statement(s) which is only executed when the condition is true.
The difference is that block belongs to if statement executes once whereas block belongs to
while statement executes repeatedly.
You can attach an optional else clause with while statement, in this case, syntax will be -
while (expression) :
statement_1
statement_2
......
else :
statement_3
statement_4
......
14. Write difference between counter control and sequential control looping
Counter controlled: loop is the one who’s end value / count known before beginning
of the program / loop. Ex: for loop
Sequential controlled: loop is the one who’s end value / count not known before
beginning of the program / loop. It is decided while execution. Ex: while loop
For while
Syntax: Syntax:
for variable_name in range(initial,
final, step value): while (expression) :
statement_1 statement_1
statement_2 statement_2
PART B
UNIT 4
PART A
List slice is same of string slice. From the list slice we can extract sub part of list
based on user requirements.
We can use Index values to create a sublist from list.
Syntax:
var_name=list_name[start value : end value: increment/decrement step value]
Python list provides many built-in function and methods for list manipulation.
These can be applied to list as per below syntax:
1. append 8. reverse
2. count 9. sort
3. extend 10. del
4. index 11. len
5. insert 12. min
6. pop 13. max
7. remote 14. sum.
Definition:
Tuples are like list , but their elements are fixed. Once you created a tuple, you
cannot add /delete/reorder/replace an element in tuple.
Tuple is immutable in nature.
Tuples can be define elements separated by comma within parenthesis.
Syntax: var_name=(element1,element2,….elementn)
Ex: tuple_values=(1,2,3,4,5,6,,7,8,9,10)
Creating empty tuple :
>>>tuple1=()
>>> tuple2=(1)
If you created tuple with single element python consider it as a value assigned and
tuple2 variable name
In order to avoid that please include “,” operator next to that element so that python
consider it as a tuple (i.e) tuple2=(1,)
7. Define dictionary
Similarity:
Difference:
You can create a dictionary by enclosing the item inside a pair of curly braces {}.
Each item consist of key, followed my colon followed by value.
Each item may be separated with commas.
Syntax: <dictionary_name>={key1:value,key2:value,…keyn:value}
Ex: Indian_player_dictionary={1:”dhoni”,2:”kholi”,3:”shewag”,4:”section”}
To create empty dictionary: <dict_name>={}
Ex:
Using for loop we can easily able to traverse the individual element in tuple
Syntax:
for var_name in range(0,len(tuple_name),1):
process each item statement
For loop is mainly used to transverse over the items of the dictionary.
Syntax: for var_name in dictionary_name:
Processing each item tree.
1. Traversing
2. Adding an element
3. Updating the elements
PART B
UNIT 5
PART A
Files are used to store data permanently. Data used in program are temporary (i.e)
lost when the program terminates or if you closed the application.
In order to save the data created in a program permanently we need to save it in a
file on a disk.
Then using that file we can later executed or used for same program.
To create a file object use open function.
To read and write data from to file use read and write function.
Mode Description
Exception handling is a process that enables a program to deal with exceptions and
continue its normal execution.
When running a program, what happens if user enter a file or value that doesn’t
exist? Suddenly the program will be aborted and raise an error.
An error that occur at runtime called exception. How can you deal with an exception
so that program can catch the error and prompt to user to enter correct filename (or)
data.
5. Define modules
As your program gets longer, you may want to split into several files for easier
maintenance.
We may also want to use a handy function that you have written in a several program
without copying its code into program.
To support this method, python has a way to put definition in a file and use them in
a program, such a file called module.
Definition from a module can be imported into another module (or) into main
module (or) for a program execution.
A module is a file containing python definition and statement. The file name is the
module name with the suffix of “.py”.
6. Define packages
Syntax:
try:
<body>
except <exception type 1>:
<handler 1>
except <exception type 2>:
<handler 2>
except <exception type n>:
<handler n>
except :<handler except>
else:
<process else>
finally:
<process finally>
Mainly three file object attributes are present to check the file opening status and
mode and name.
Attribute Description
file.closed Return true if the file in closed false otherwise
file.mode Return the mode with which file was opened.
file.name Return name of the file
close() Close an open file. It has no effect if the file is already closed.
Read atmost n characters form the file. Reads till end of file if it is
read(n)
negative or None.
PART B
1. Explain in detail about text files and its mode of its operations.
2. Explain in detail about reading of data from file
3. Explain in detail about writing data in to file
4. Explain in detail about exceptional handling
5. Explain in detail about modules & packages
6. Explain command line arguments in detail
7. How will you create user defined exceptions
8. Write a program to copy the contents of one file to another file
9. Write a program to find most frequent word occurrence
10. Write a program to find word count & letter count