Professional Documents
Culture Documents
(b). By typing the string in triple quotation marks. No backslash needed at the end of line.
Address = ‘’’Seawood
Darawe
Navi
Mumbai’’’
What are numeric literals?
Ans.: A numeric literals represents any number. Python has three numeric literals.
I. int e.g. 7, 58, 231, -25, +54 etc.
II. float e.g. 25.23, -50.25, 145.0015, etc.
III. complex e.g. a + bj, where a and b are floats and j represents √-1, which is an imaginary
number. The a is the real part of the number, and b is the imaginary part.
Bitwise Operators:
& Bitwise AND ^ Bitwise exclusive OR (XOR) | Bitwise OR
Logical Operators:
and Logical AND or Logical OR
Assignment Operators:
= Assignment
/= Assign Quotient
*= Assign Product
+= Assign Sum
-= Assign Difference
%= Assign Remainder
**= Assign Exponent
//= Assign Floor Division
Relational Operators:
< Less than > Greater than
<= Less than or equal to >= Greater than or equal to
== Equal to != Not equal to
Shift Operators:
<< Shift Left >> Shift Right
Identity Operators:
is is the identity same? is not is the identity not same?
Membership Operators:
in whether variable in sequence not in whether variable not in sequence
Operand: Variables and constants together are called operand. It is the operand on which the
operators work upon.
#Program to enter length and breadth of rectangle and theb find its area & perimeter
length = float(input('Enter The Length of Rectangle: '))
breadth = float(input('Enter The Breadth of Rectangle: '))
area = length * breadth
perimeter = 2 * (length + breadth)
print('The area of reactangle of length',length,'and breadth',breadth,'is = ',area)
print('The area of rectangle of length',length,'and breadth',breadth,'is = ',perimeter)
Program 6:
#Program to enter marks of 5 subjects and then calculate total & percent.
phy = float(input('Enter marks of Physics: '))
chem = float(input('Enter marks of Chemistry: '))
maths = float(input('Enter marks of Maths '))
eng = float(input('Enter marks of English: '))
comp = float(input('Enter marks of Computer: '))
total = phy + chem + maths + eng + comp
percent = (total * 100) / 500
print('Total marks scored is = ',total)
print('Percentage scored is = ',percent)
What are comments?
Ans.:
Comments are used to give additional information about something in the program like
variables, objects, functions, blocks, expressions, statements, etc.
It helps users and programmers to understand the program better.
Comments are non executable i.e. interpreter ignores it while executing the program.
There are two types of comments:
Full line comments: It starts with # symbol followed by comments and gets over in a line.
e.g. #Let us learn Python programming.
Multi-line comments: It starts and end with triple-apostrophe(‘ ‘ ‘) or triple quote (“ “ “) followed
by comments. This type of comment is also known as docstring.
e.g. “ ” ”Comments are used to give additional information about something in the program
like variables, objects, functions, blocks, expressions, statements, etc.” ” ”
What is statement?
Ans. An statement is a command or instruction given to the computer to perform some task. An
statement is executed and it is not necessary that a statement results in a value. e.g.
print(“This is Python Programming”)
What are expressions?
Ans. An expression is any legal combination of symbols that represents a value. It can be value
only or it can be complex expressions that produces a value. e.g.
25 # Simple Expression
3.78 # Simple Expression
a+b # Complex Expression
(a + 3) * 7 # Complex Expression
What are blocks in Python?
Ans. A group of statements which are part of another statement or a function are called block or
code-block or suite in Python.
if N1>N2:
Diff=N1-N2
print("The Two Number Are: ",N1," and ",N2)
print("Difference is: ",Diff)
else : Block
Sum=N1+N2
print("The Two Number Are: ",N1," and ",N2)
print("Sum is: ",Sum)
What is a variable in Python?
Ans. A variable in Python represents named locations that refers to a value and whose value can
be used and processed during program run. E.g. In Program 2, N1, N2, Diff and Sum are variables.
What is the use of type( ) function in Python?
Ans. This function is used to determine the type of an object( variables and literals).
>>> a = 9
>>> type(a)
<class 'int'>
>>> a = 'Python'
>>> type(a)
<class 'str'>
>>> a = 2.5
>>> type(a)
<class 'float'>
>>> b = 5
>>> c = a < b
>>> type(c)
<class ‘bool’>
What is the use of input( ) function in Python?
Ans. This is built in function used to get a value from the user and assign to a variable.
Syntax:
Variabel_Name = input(“Message to be displayed for user”)
e.g. Name = input(“Enter Your Name”)
Note: This function always returns a value of string type. So when you are entering any number
and want to perform some mathematical or logical operation then this number must be
converted into number format before performing the mathematical or logical operation
otherwise interpreter will generate an error.
N1 = input("Enter a Number: ")
Next = N1+1 # N1 is in string format
print("The next number is: ",Next)
This error can be resolved in two ways as follows:
(i) N1 = input("Enter a Number: ")
N1 = int(N1) # N1 is converted to integer format
Next = N1+1
print("The next number is: ",Next)
print("My","Name", "Is","Krishna.",sep="...",end="*")
Print()
print("My","RollNumber","Is","15",sep=".....",end="**")
Print()
print("I","Like","To","Play","Cricket",sep=".....",end="***")
My...Name...Is...Krishna.*
My.....RollNumber.....Is.....15.**
I.....Like.....To.....Play.....Cricket.***
>>> 2 + 3 >>> print("Deepak's Mobile")
>>> 5 – 2 >>> print('Deepak\'s "Mobile“ ')
>>> 4 * 5 >>> print('“Deepak\'s Mobile“ ')
>>> 10 / 2 >>> 'Python' + ' Python' + ' Python‘
>>> 10 // 2 >>> 5 * 'Python ‘
>>> 15 / 2 >>> print("c:\Python\navin's programming")
>>> 15 // 2 >>> print("c:\Python\Navin's Programming")
>>> 15 % 2 >>> print(r"c:\Python\navin's programming")
>>> 15 % 3 >>> print(r"c:\Python\Navin's programming")
>>> 3 ** 5 >>> N1 = 5
>>> 2 + 5 – 3 >>> N2 = 10
>>> (8 + 3) * 2 >>> N1 + N2
>>> “Python” >>> _ * 2
>>> ‘Python’ >>> Program = 'MyProgram'
>>> print(‘Python’) >>> Program
>>> print(‘Deepak’s Mobile’) >>> Program + ' of Python'
>>> print(‘Deepak\’s Mobile’)
>>> Program[0]
-9 -8 -7 -6 -5 -4 -3 -2 -1
>>> Program[1]
>>> Program[2] M y P r o g r a m
>>> Program[8] 0 1 2 3 4 5 6 7 8
>>> Program[9]
>>> Program[-1] >>> a = 5
>>> Program[-2]
>>> b = 10
>>> Program[-9]
>>> a < b
>>> Program[-10]
>>> Program[0:2] True
>>> Program[0:5] >>> a > b
>>> Program[2:9] False
>>> Program[2:] >>> int(True)
>>> Program[:2] 1
>>> Program[:9] >>> int(False)
>>> Program[0:15] 0
Data Types
Integer Float Complex Boolean String List Tuple Set Range Dictionary
Integers Signed: It is the normal integer representation of whole numbers. The numbers can
be positive as well as negative.
e.g. 25, 68, 548, +85, -57, etc.
Booleans: It represents the truth values True (1) and False (0).
STRING[3:15]
'PROGRAM‘
STRING[10:20]
‘‘
STRING[-15:15]
'MY PROGRAM‘
Note: There are certain cases where Python creates different objects even if the values
are same for these objects. These are:
When any string is entered i.e. more than a character.
When entered number is greater than integer 256.
When any float or complex number is entered.
Bitwise Operators: Bitwise operators are similar to logical operators, except that these
operators work on binary representation of data.
>>> ~12 ( 1 2 ) 10 = 0 0 0 0 1 1 0 0 ( 1 3 ) 10 = 0 0 0 0 1 1 0 1
-13 11110011 11110010 1’s Complement
+ 1 2’s Complement
1 1 1 1 0 0 1 1 -13
>>> 12 | 13 ( 1 2 ) 10 = 0 0 0 0 1 1 0 0 X Y X|Y
13 ( 1 3 ) 10 = 0 0 0 0 1 1 0 1 0 0 0
00001101 0 1 1
1 0 1
1 1 1
X Y X&Y
0 0 0
>>> 12 & 13 ( 1 2 ) 10 = 0 0 0 0 1 1 0 0 0 1 0
12 ( 1 3 ) 10 = 0 0 0 0 1 1 0 1 1 0 0
00001100 1 1 1
X Y X^Y
0 0 0
>>> 12 ^ 13 ( 1 2 ) 10 = 0 0 0 0 1 1 0 0 0 1 1
1 ( 1 3 ) 10 = 0 0 0 0 1 1 0 1
1 0 1
00000001 1 1 0
Logical Operators: These operators are used to combine two or more expressions
together.
or With this operator if the first expression is true then the remaining
(Logical or) expression are not checked. Result is True if any one expression is
true; False if all the expressions are false.
and With this operator if the first expression is false then the remaining
(Logical and) expression are not checked. Result is False if any one expression is
false; True if all the expressions are true.
not The logical operator negates or reverses the truth value of the
(Logical not) expression.
Operator Precedence
Operator Operator’s Name
Highest
() Parentheses
** Exponentiation
~x Bitwise not
+x, -x Unary Plus, Minus
2/6*5//5%10 *, /, //, % Arithmetic
2+3*5-2*5/3 +, - Arithmetic
2*3**2 & Bitwise AND
2**2**3 ^ Bitwise XOR
| Bitwise OR
<, <=, >, >=, <>, !=, ==, is, is not Relational Operators, Identity Operators
Statement 1 Statement 2
Next Statement
What is iterative statement flow of control in Python?
Ans. The iterative statements allow a set of instructions to be executed
repeatedly until a certain condition is fulfilled. The iteration statements are
also called looping statements.
TRUE FALSE
Condition
Exit the Loop
Statement 1
Body of Loop
Statement 2
If statement : Flow Chart SYNTAX:
Start
if(test expression / condition):
statement/s
FALSE statement-x
TRUE
Condition
Statement/s
End
If - else statement : Flow Chart SYNTAX:
Statement 2 Statement 1
End
The range() Function: The range() function of Python generates a list which is a
special sequence type.
Syntax:
range(LowerLimit, UpperLimit, StepValue)
range(1, 10, 1) will generate a list as [1, 2, 3, 4, 5, 6, 7, 8, 9]
range(1, 10, 2) will generate a list as [1, 3, 5, 7, 9]
range(2, 10, 3) will generate a list as [2, 5, 8]
range(5, 0, -1) will generate a list as [5, 4, 3, 2, 1]
Note: Lower limit and step value are optional.
range(1, 5) will generate a list as [1, 2, 3, 4] where step value is not passed in
the range() function. By default the step value is 1.
range(5) will generate a list as [0, 1, 2, 3, 4] where lower limit and step value are
not passed. By default the lower limit value is 0.
Iteration/Looping /Repetition Statements: The iteration or repetition statements
allow a set of instructions to be performed repeatedly until a certain condition is
fulfilled.
There are two types of looping statements in Python:
i.for Loop: The for loop of Python is designed to process the items of any
sequence, such as a list, tuple or string one by one. The for loop is also called as
counting loop because it repeats the statements for certain number of times.
Syntax:
for <variable> in <sequence>:
body of loop
or statements to be repeated
e.g.
for a in range(1, 11): # Each time loop variable a will be assigned
print(a) # a value from 1 to 10 and then printed
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
ii. while Loop: The while loop is a conditional loop that will repeat the instructions
within itself as long as a condition remains true.
Syntax:
initialization
while <condition>:
body of loop
or statements to be repeated
update expression
e.g.
a=1 # initialization
while a<=10 : # condition
print(a) # body
a += 1 # update expression
Attendance = " "
Day = 1
Present = 0
Absent = 0
Choice = "Y"
while Choice == "Y" or Choice == "y":
print("Enter attendance of day ",Day, ":",end =" " )
Attendance = input()
if Attendance =="P" or Attendance == "p":
Present +=1
Day+=1
else:
Absent +=1
Day+=1
Choice = input("Do you want to enter the attendance for next Day? Press Y/N")
if Day>6:
print("You have entered one week attendance")
break