Python QB ct1
Python QB ct1
Easy to code:-
Python is a high level language. Python is very easy to learn the
language as compared to C, C++, Javascript, Java , etc.
It is very easy to code in python as the syntax is comparatively
easy. It is also a developer friendly language.
Numeric:-
Numeric value can be integer, floating number or even complex
numbers.
Integers – This value is represented by int class. It contains
positive or negative whole numbers (without fraction or decimal). In
Python there is no limit to how long an integer value can be.
Float – This value is represented by float class. It is specified by a
decimal point.
Complex Numbers – Complex number is represented by complex
class. It is specified as (real part) +
(imaginary part)j. For example – 2+3j
String
In Python, Strings are arrays of bytes representing Unicode
characters. A string is a collection of one
or more characters put in a single quote, double-quote or triple
quote. In python there is no character
data type, a character is a string of length one. It is represented
by str class.
1. List
Lists are just like the arrays, declared in other languages which
is a ordered collection of data. It is very
flexible as the items in a list do not need to be of the same type.
2. Tuple
Just like list, tuple is also an ordered collection of Python
objects. The only difference between tuple and
list is that tuples are immutable i.e. tuples cannot be modified
after it is created. It is represented by tuple
class.
Example:-
my_tuple = (1,2,3,4,5)
my_list = [1,2,3,4,5]
my_set = {"1":"ONE",
"2": "TWO"}
my_int = 10
my_float = 12.12
my_string = "Hello"
print("List",my_list)
print("Tule",my_tuple)
print("Integer",my_int)
print("Float",my_float)
print("String",my_string)
print("set",my_set)
3. Compare Interactive mode and Script mode.
Variables:-
Variable is a name that is used to refer to memory location.
Python variable is also known as an
identifier and used to hold value.
In Python, we don't need to specify the type of variable because
python is a dynamically typed language.
Variable names can be a group of both the letters and digits,
but they have to begin with a letter or
an underscore.
It is recommended to use lowercase letters for the variable
name. Rahul and rahul both are two
different variables.
KeyWords:-
Python Keywords are special reserved words that convey a
special meaning to the compiler/interpreter.
Each keyword has a special meaning and a specific operation.
These keywords can't be used as a variable.
Comments
Python Comment is an essential tool for the programmers.
Comments are generally used to explain the
code. We can easily understand the code if it has a proper
explanation.
Multiline comment:-
''' This is a multiline comment It spans multiple lines Python will
ignore these lines when executing
the code '''
Indentation:-
Python indentation refers to
adding white space before a statement to a particular block of
code. In another word, all the statements
with the same space to the right, belong to the same code
block. To indicate a block of code in Python,
you must indent each line of the block by the same whitespace.
Example:-
j=1
while(j<= 5):
print(j)
j=j+1
#this is an indentation
5. Write steps to install python and run the python code:-
1. Download Python:
• Visit the official Python website:
https://www.python.org/downloads/.
• Click on the "Downloads" tab and choose the latest
version of Python for Windows.
• Download the installer (usually a .exe file).
2. Run the Installer:
• Double-click on the downloaded .exe file.
• Check the box that says "Add Python x.x to PATH" during
installation.
• Click "Install Now."
3. Verify Installation:
• Open a command prompt (cmd) and type python --
version or python -V. It should display the installed
Python version.
They are used to check if two values (or variables) are located on
the same part of the memory or not. Two variables that are equal
does not imply that they are identical
Example:-
Membership operators:-
They are used to test whether a value or variables is found in a
sequence or not.
in operator:-
returns True if the value/variable is found in the sequence
Syntax:- element in sequence
not in operator:-
returns True if the value/variable is not found in the sequence
Syntax:- element not in sequence
Logical Operators
Exmaple:-
x1 = True
x2 = True
if x1 and x2:
print("Logical And is true")
print("Use of Logical Not",not x1)
Syntax:-
if expression 1:
# block of statements
elif expression 2:
# block of statements
elif expression 3:
# block of statements
else:
# block of statements
Example:-
score = int(input("Enter your score: "))
if score >= 90:
grade = 'A'
elif score >= 80:
grade = 'B'
elif score >= 70:
grade = 'C'
elif score >= 60:
grade = 'D'
else:
grade = 'F'
print("Your grade is:", grade)
for i in range(2,19,2):
print(i, end=" ")
Example:-
j=1
while(j<= 5):
print(j)
j=j+1
#this is an indentation
6. Explain use of pass and else keywords with for loops in python
Pass statement:-
● It is generally used as a placeholder for future code i.e. in cases
where you want to implement some part of your code in the future
but you cannot leave the space blank as it will give a compilation
error.
● Sometimes, pass is used when the user does not want any code
to execute.
● Using the pass statement in loops, functions, class definitions,
and if statements, is very useful as empty code blocks are not
allowed in these.
Else statement:-
The else block is placed just after the for or while loop.
It is executed only when the loop is not terminated by break
statement.
Else statement with for loop
For variable in sequence:
Body of for loop
else:
statement
Example of pass and else :-
for i in range(2,19,2):
if i == 4:
pass
print(i)
else:
print("This is the statement after loop is completed and not
terminated by break")
7. Mention the use of //, **, % operator in python
// - Floor Division -
The floor division operator divides on value by second value and
gives their quotient rounded to the next smallest whole number
Example:-
x1 = 11
x2 = 32
print("Floor Division", x2//x1)
** - Exponential
The exponential operator raises on value to power of second value
Example:-
x1 = 10
x2 = 2
print("Exponentioal operator", x1**x2)
% - modulus operator
The modulus operator divides one value by second value and
gives the remainder
Example:-
x1 = 10
x2 = 2
print("Modulus operator", x1%x2)
Example:-
a, b = 10, 20
print(a == b) # since the values 10 and 20 are different, the output
of this print statement is False
print(a != b) # since both the values are not equal, the output of
this print statement is True
print(a > b) # the value 10 is less than 20, hence the output of this
print statement is False
print(a < b) # since the value 10 is less than 20, the output of this
print statement is True
print(a >= b) # Since the value 10 is less than 20, the output of this
print statement is False
print(a <= b) # Since the value 10 is less than 20, the output of this
print statement is True
9. Write a program for else if ladder in python
score = int(input("Enter your score: "))
if score >= 90:
grade = 'A'
elif score >= 80:
grade = 'B'
elif score >= 70:
grade = 'C'
elif score >= 60:
grade = 'D'
else:
grade = 'F'
print("Your grade is:", grade)
12.Write a program to explain the use of pass and break in for loop.
for i in range(1,10):
if i == 7:
break
elif 1 == 2:
pass
else:
print(i)
print("This is after break statement")
13. Give difference between list and tuple.
List Tuple
List is mutable Tuple is immutable
List are enclosed with “[]” Tuple are enclosed with “()”
List consume more memory Tuples consumes less memory
as compared to lists
List better for performing A tuple data type is appropriate
operations such as insertion and for accessing the elements
deletion
Syntax:-
Slice = <ListName>[startIndex : endIndex : step]
The start index represents the value from where the slicing should be
started or begin. By default value is 0.
The stop index represents the last index up to which the list slicing
will go on. The default value is (length(list)-1)
1. Union
2. Intersection
3.Difference
4.symmetric difference
set1 = {1,2,3,4,5,6}
set2 = {1,2,4,5,6,0,9,11,12}
print("Union", set1|set2)
print("Intersection",set1&set2)
print("Difference" ,set1-set2)
print("Symmetric difference", set1^set2)
16.Write a program to create dictionary of students that includes
their Roll no and Name.
i) Add three Students in above dictionary.
ii) Update name='meeta' of Roll n0=3
ii) Delete information of Roll no=2
my_dict = {1:"Bhumika",
2:"Jiya",
3:"Mohit",
4:"Palak"}
print("Before updating roll no. 3", my_dict)
my_dict[3]="meeta"
print("After updating roll no. 3",my_dict)
my_dict.pop(2)
print("After deleting the info of roll no. 2",my_dict)
my_dict = {1:"Bhumika",
2:"Jiya",
3:"Mohit",
4:"Palak"}
print("Original dict:",my_dict)
my_dict.pop(2)
print("Using pop and removing roll no 2",my_dict)
my_dict[5]=["Naish"]
print("Adding roll no 5",my_dict)
my_dict.update({1:"Bhumika Bhatia"})
print("Updating roll no 1",my_dict)
print("Values of dict", my_dict.values())
print("Keys of dict", my_dict.keys())
print("Items of dict", my_dict.items())
my_dict2 = my_dict.copy()
print("Created a copy of my_dict",my_dict2)
x = {2,3,4}
y = {0}
my_dict = dict.fromkeys(x,y)
print("from keys",my_dict)
print("CLearing the dict",my_dict.clear())