002 - 08-05-2020 XI-Python Basics - Programming #1
002 - 08-05-2020 XI-Python Basics - Programming #1
Computer Science
Programming with Python - 1
Literals - the data in itself 2
4 is an int
4.0 is a float
You may think that they are exactly the same, but Python sees them in a
completely different way.
It is also important to note that it is not only a decimal point which makes
a numerical value float. A float value can also be represented using the
exponent notation E. For example 4E8 is a float value representing 4 x
108
Strings 5
● Strings are used when you need to process text (like names of all
kinds, addresses, novels etc.), not numbers.
● Strings need quotes the way float need points.
● You may enclose a string either within a set of single quotes or double
quotes.
"This is a string"
'This is a string'
"It's Correct"
'I am "Monty Python"'
"I am 'Monty Python'"
Boolean values 6
"207"
a. int
b. float
c. str
d. bool
Ans:
c. str
Quiz 8
a. str
b. float
c. int
d. bool
Arithmetic operators 9
Python uses the following arithmetic operators upon different type of data
depending upon compatibility of the operator with the literal upon which it
is used:
Pno Name Symbol Example
Result
1. Exponential ** 3**2
9
6.25 ** 0.5
2.5
2. Multiplication * 4 * 3
12
2.5 * 2
5.0
Arithmetic operators 10
Pno Name Symbol Example
Result
3. Division / 3/2
1.5
6.0/3
2.0
4. Floor Division // 7 // 2
3
5 //
2.0 2.0
5. Remainder/Modulo % 7 % 2
1
7.0 % 5
2.0
Precedence of Operators 11
Se Operators Se Operators
q q
1 () 5 + - (Binary Operators)
2 ** 6 <= < > >=
3 + - (Unary Operators) 7 == !=
4 * / % // 8 = %= /= //= -= += *= **=
Evaluation of arithmetical expression 12
2 + 3 * 5 17
1 2
(2 + 3) * 5 25
3 1 4 2
>>> 2 ** 3 ** 2 >>>2**(3**2)
512
512
>>>(2**3)**2
64
1 2
>>> 16 % 5 % 3 >>>(16 % 5) % 3
1 1
>>>16 % (5 % 3)
0
Interesting Facts of Arithmetic 14
Operations
● A division operator / always gives the result of division in float i.e.
with a decimal point irrespective of whether the operands are integer
or float.
● A floor division // operator gives the nearest smaller integer of the
division result. (If Numerator or Denominator is float, the result is float equivalent
to the int result)
● A modulo operator % evaluates the result of remainder of the division
operation upon the two operands for example Numerator %
Denominator, is always calculated using the convention:
Numerator = Quotient * Denominator + Remainder
or, Remainder = Numerator - Quotient * Denominator
where Quotient is the result of the Numerator // Denominator
(Note the floor division)
Interesting Facts of Arithmetic 15
Operations
>>> 7 / 2 >>> -7 // 2
3.5 -4
>>> 4 / 2 >>> -7 % 2
2.0 1
>>> 7 // 2 >>> 7 % -2
3 -1
>>> 7.0 //2 >>> 7.5 % -2
3.0 -0.5
>>> -7 / 2 >>> - 7.5 % 2
-3.5 0.5
>>> 7 % 2
1
Interesting Facts of Arithmetic 16
Operations
Strings can be operated with the arithmetic operators * and
+
With multiply operator *, if one With the addition operator +, if one
of the operands is a string then of the operands is a string the the
the other operand must be an other operand must also be a
integer. In such a case the string string. In such a case the first
is replicated the integer number string operand is concatenated
of times. Example: with the second string operand.
>>> "Hello" * 5 Example:
'HelloHelloHelloHelloHello' >>> "Hello" + "Class 11"
>>> "*" * 20 'HelloClass 11'
'********************' >>> "Welcome "+"To "+"Python"
'Welcome To Python'
Variables - What are variables? 17
The type of the value assigned to a created variable decides the type of
the variable:
>>> type(value)
<class 'int'>
>>> type(client_name)
<class 'str'>
Using Variables 23
However, you are not allowed to use a variable which has not been
created i.e. which does not have a value assigned to it. For example the
following statement will throw an error:
>>> Age = 25
>>> type(age)
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
type(age)
NameError: name 'age' is not defined
The reason of the error above is that we tried to find type of variable age,
though the variable which we had created was Age.
Using Variables 24
>>> Value="Welcome"
>>> type(Value)
<class 'str'>
>>> Value = 25
>>> type(Value)
<class 'int'>
>>> Value = 4/2
>>> type(Value)
Can you guess why the type
<class 'float'> of Value is float here ?
Using Variables 25
Find the outputs of the following code for the red texted
commands:
>>>ver = "3.8" 'Python version3.8'
>>>"Python version" + ver
>>>X=3
>>>Y=2 6561
>>>X**Y**X
>>>Z=X+Y '3.83.83.83.83.8'
>>>ver*Z <class 'str'>
>>>type(ver*Z)
>>>Z = X // Y 1
<class 'int'>
>>>Z
>>>type(Z)
28
Thank You
Computer Science Department
Delhi Public School, R.K.Puram