0% found this document useful (0 votes)
92 views8 pages

Python Key

The document discusses various Python concepts including operators, sequence types, functions, and more. It provides examples of different types of operators in Python like arithmetic, assignment, comparison, and bitwise operators. It also lists six different types of sequence types like strings, ranges, lists, tuples, dictionaries and sets. Additionally, it defines concepts like the self variable in classes, mathematical functions in the math module, and summarizes control structures like if-elif-else and for loops.

Uploaded by

koushik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views8 pages

Python Key

The document discusses various Python concepts including operators, sequence types, functions, and more. It provides examples of different types of operators in Python like arithmetic, assignment, comparison, and bitwise operators. It also lists six different types of sequence types like strings, ranges, lists, tuples, dictionaries and sets. Additionally, it defines concepts like the self variable in classes, mathematical functions in the math module, and summarizes control structures like if-elif-else and for loops.

Uploaded by

koushik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

PART-A

1. A) List the operators in python.


Ans)
Operators are used to perform operations on variables and values.
Python divides the operators into seven types:
 Arithmetic operators
 Assignment operators
 Relational/Comparison operators
 Logical operators
 Identity operators
 Membership operators
 Bitwise operators
B) What are the various bitwise operators in python.
Ans)
Bitwise Operators: will work on binary numbers. There are 6 bitwise operators in Python.
 Bitwise AND Operator (&) :
if both binary bits are 1 returns 1, otherwise 0
 Bitwise OR Operator (|):
if both binary bits are 0 returns 0, otherwise 1
 Bitwise XOR Operator (^):
if both binary bits are 0 or 1 returns 0, otherwise 1
 Bitwise Ones’ Compliment Operator (~):
if a number ‘A’ is equal to -(A+1)
 Bitwise Left Shift operator (<<):
it shifts the left operand bits towards the left side for the given number of
times in the right operand. In simple terms, the binary number is appended with 0s at
the end.
 Bitwise Right Shift Operator (>>):
left side operand bits are moved towards right side for the given number of
times. In simple terms, the right side bits are removed
C) What are six different types of sequence types in python?
Ans)
 Strings
Ex: a= "Hello, world!"
Print(a[0])
Output: 'H'
 Range
Ex: for i in range(0,5):
print(i,end=” “)
Output: 1 2 3 4 5
 Lists
Ex: a=[1,2,3]
Print(a)
Output: [1, 2, 3]
 Tuples
Ex: a=(1,2,3)
Print(a)
Output: (1, 2, 3)
Containers of sequential data
 Dictionaries
 Set
D) State the role of PIP command
Ans) pip is a package manager for Python packages. Pip command is used to install and
uninstall Python packages. When we install pip, it is added to the system as a command
line program which can be run from the command line.
Install:
Pip <install> package_name
uninstall:
Pip <uninstall> package_name
E) Define Self variable method.
Ans) self represents the instance of the class. By using the “self” keyword we can access
the attributes and methods of the class in python. It binds the attributes with the given
arguments.
self is parameter in function and user can use another parameter name in place of it.
Ex:
class Demo:
def show(self):
print("using self variable")

ob = Demo()
ob.show()
output:
using self variable
F) State any three mathematical functions in python.
Ans)
Math module is used to define mathematical functions, trigonometric functions,
logarithmic functions, angle conversion functions. If is a predefined module. Module can
be used by using “import” statement.
Ex:
import math
print(math.pi) //prints mathematical constant value
print(math.pow(2,4)) // performs 2^4
print(math.sqrt(100)) //square_root of 100
output:
3.141592653589793
16.0
10.0

PART-B
1. A) Elaborate the need of python programming in detail and python script
Ans)
Internet scripting
Database Programming
Software quality
Developer productivity
Component integration
It's Object-Oriented
Python is an object-oriented language. Its class model supports advanced
notions such as polymorphism, operator overloading, and multiple inheritance. Object-
oriented programming (OOP) is easy to apply. Python programs can subclass classes
implemented in C++ or Java.
Enjoyment
Python's built-in toolset, learning of python programming is more easy for
users.
It's Free
Python is freeware also called as open source software. There are no
restrictions on copying it, embedding it in your systems, or shipping it with your
products.
It’s Portable
Python is written in portable ANSI C. it runs on UNIX systems, Linux, MS-
DOS, MS-Windows (95, 98, NT), and more. Python programs are automatically
compiled to portable byte code, which runs the same on any platform with a compatible
version of Python installed.
It's Powerful
Python is a hybrid means its tool is set places in between traditional scripting
languages (such as Tcl, Scheme, and Perl), and systems languages (such as C, C++, and
Java). Python provides all the simplicity and ease of use of a scripting language, along
with more advanced programming tools.
Automatic memory management
Python automatically allocates and reclaims ("garbage collects") objects when
no longer used, and most grow and shrink on demand.
It's Mixable
Python programs can be easily "glued" to components written in other
languages. Python programs can be both extended by components written in C or C++,
and embedded in C or C++ programs.
It's Easy to Use
To run a Python program, simply type it and run it. There are no intermediate
compile and link steps (as when using languages such as C or C++). Python programs
are compiled (translated) to an intermediate form called bytecode, which is then run by
the interpreter.
It's Easy to Learn
compared to other programming languages python language is easy to learn.
Support libraries
Python comes with a large collection of prebuilt and portable functionality,
known as the standard library.
1. B) Write a python program to read number ‘n’ and find the sum of odd
numbers till the given number ‘n’.
Ans)
Program:
n=10
sum=0
for i in range(1,n+1):
if(i%2==1):
print(i)
sum=sum+i
print(“sum is:”,sum)
Output:
sum is:25
2. A) Write short notes on
i) Identity operators
ii) If-elif-else structure
iii) For loop & For each
iv) Range function
Ans)
i) Identity operator: Identity operators compare the memory locations of two objects.
There are two Identity operators
 Is: Evaluates to true if the variables on either side of the operator point to the
same object and false otherwise.
 is not: Evaluates to false if the variables on either side of the operator point to
the same object and true otherwise.
Example:
a = 20
b = 20
if ( a is b ):
print("a and b have same identity")
else:
print(" a and b do not have same identity")
if ( id(a) == id(b) ):
print(" a and b have same identity")
else:
print("a and b do not have same identity")
Output
a and b have same identity
a and b have same identity
ii) If-elif-else structure:
If the condition for if is False, it checks the condition of the next elif block and so on. If
all the conditions are False, body of else is executed. Only one block among the several
if...elif...else blocks is executed according to the condition. The if block can have only one
else block. But it can have multiple elif blocks.
Syntax:
if test expression:
Body of if
elif test expression:
Body of elif
else:
Body of else
Flowchart:

Program:
num = 3.4
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
Output:
Positive number
iii) For loop & For each: It has the ability to iterate over the items of any sequence, such as a
list, tuple or a string. For each is nothing but for loop iterate on sequence.
Syntax:
for iterating_var in sequence:
statements(s)
Flowchart:
Program:
for i in 'Python':
print(i)

fruits = ['banana', 'apple', 'mango']


for i in fruits:
print(i)
Output:
P
y
t
h
o
n
banana
apple
mango

iv) Range function: range() allows user to generate a series of numbers within a given range.
Depending on how many arguments user is passing to the function, user can decide where
that series of numbers will begin and end as well as how big the difference will be between
one number and the next. range() takes mainly three arguments.
 start: integer starting from which the sequence of integers is to be returned
 stop: integer before which the sequence of integers is to be returned.
The range of integers end at stop – 1.
 step: integer value which determines the increment between each integer in the
sequence
Example:
for i in range(10):
print(i, end =" ")
print()

l = [10, 20, 30, 40]


for i in range(len(l)):
print(l[i], end =" ")
print()

sum = 0
for i in range(1, 10):
sum = sum + i
print("Sum of first 10 natural number :", sum)

Output:
0123456789
10 20 30 40
Sum of first 10 natural number: 45
2 B) Write a python program to generate the first 30 multiples of 5.
Ans)
Program:
n=int(input("enter value:"))
print("first 30 multiples of 5")
for i in range(1,n+1):
if(i%5==0):
print(i,end=" ")
Output:
enter value:150
first 30 multiples of 5
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135
140 145 150

3 A) Explain the following with respect to dictionaries


a) Creation
b) Update
c) Iterators in Dictionaries
d) Dictionary Comprehension
Ans)
Dictionary:  it is an unordered collection of data values, used to store data values like a map,
which unlike other Data Types that hold only single value as an element, Dictionary
holds key : value pair. Key value is provided in the dictionary to make it more optimized.
Each key-value pair in a Dictionary is separated by a colon :, whereas each key is separated
by a ‘comma’.
a) Creation: In Python, a Dictionary can be created by placing sequence of elements
within curly {} braces, separated by ‘comma’.
Values in a dictionary can be of any datatype and can be duplicated, whereas
keys can’t be repeated and must be immutable.
Dictionary can also be created by the built-in function dict(). An empty
dictionary can be created by just placing to curly braces{}.
Example:
Dict = {}
print("Empty Dictionary: ")
print(Dict)

Dict = {1: 'python', 2: 'ds', 3: 'co'}


print("\n Dictionary with the use of Integer Keys: ")
print(Dict)

Output:
Empty Dictionary:
{}

Dictionary with the use of Integer Keys:


{1: 'python', 2: 'ds', 3: 'co'}

b) Update: In Python Dictionary, Addition of elements can be done in multiple ways.


One value at a time can be added to a Dictionary by defining value along with the key
e.g. Dict[Key] = ‘Value’.
Updating an existing value in a Dictionary can be done by using the built-in
update() method. Nested key values can also be added to an existing Dictionary.
Example:
Dict = {}
print("Empty Dictionary: ")
print(Dict)

Dict[0] = 'python'
Dict[2] = 'ds'
Dict[3] = 1
print("\nDictionary after adding 3 elements: ")
print(Dict)
Output:
Empty Dictionary:
{}

Dictionary after adding 3 elements:


{0: 'python', 2: 'ds', 3: 1}
c) Iterator in Dictionaries:

You might also like