Literals and operation
Literals – the data in itself
A literal is data whose values are determined by the
literal itself.
Can you guess what value it represents?
123 , c
123 is a literal, and c is not.
The number is converted into machine
representation (a set of bits).
integers, that is, those which are devoid of the fractional
part;and floating-point numbers (or simply floats), that
contain (or are able to contain) the fractional part.
Convert integer to binary
4 is written as 100 in binary and stored
Number representation
Binary
Octal
hexadecimal
octal
If an integer number is preceded by an 0O or 0o prefix
(zero-o), it will be treated as an octal value. This means
that the number must contain digits taken from the
[0..7] range only.
Print(0o123)
o/p 83
hexadecimal numbers.
Such numbers should be preceded by the
prefix 0x or 0X (zero-x).
print(0x123)
o/p 291
Literals – floats
They are the numbers that have (or may have) a fractional
part after the decimal point.
you can omit zero when it is the only digit in front
of or after the decimal point. Eg 0.4 as .4
4 is an integer number
4.0 is a floating-point number
When you want to use any numbers that are very large or
very small, you can use scientific notation.
3 × 108 as 3E8
Note:
the exponent (the value after the E) has to be an integer;
the base (the value in front of the E) may be an integer.
print(0.000000000000000005)
o/p: 5e-18
Print(3e8)
o\p i am “RITU”
print("i am \"RITU\"")
print(‘i am \"RITU\“’)
Boolean value.
True false
If a==true:
Operators – data manipulation tools
Python can be used as a calculator.
An operator is a symbol of the programming language,
which is able to operate on the values.
Data and operators when connected together form
expressions. The simplest expression is a literal
itself.(+-/%**)
Arithmetic operators – exponentiation
A ** (double asterisk) sign is an exponentiation (power)
operator.
Its left argument is the base, its right, the exponent.
print(2**3)
print(2.**3)
print(2**3.)
print(2.**3.)
when both ** arguments are integers, the result is
an integer, too;
when at least one ** argument is a float, the result is
a float, too.
Print 4 th root of 2..??
multiplication
An * (asterisk) sign is a multiplication operator.
print(2*3)
print(2.*3)
print(2*3.)
print(2.*3.)
division
A / (slash) sign is a divisional operator. The value in front
of the slash is a dividend, the value behind the slash,
a divisor.
Print(6/2)
The result produced by the division operator is
always a float, regardless of whether or not the result
seems to be a float
A // (double slash) sign is an integer divisional
operator. It differs from the standard / operator in two
details:
its result lacks the fractional part – it’s absent (for
integers), or is always equal to zero (for floats); this means
that the results are always rounded;
it conforms to the integer vs float rule.
Print(6//3)
Print(6.//3)
integer by integer division gives an integer result.
All other cases produce floats.
Print(6//4)
Print(6.//4)
The result of integer division is always rounded to the
nearest integer value that is less than the real (not
rounded) result.
This is very important – rounding always goes to the
lesser integer.(3.8 to 3)
Print(-6//4)
Print(6.//-4)
remainder
The result of the operator is a remainder left after the
integer division.
the operator is sometimes called modulo in other
programming languages.
Print(3%2)
Print(12%4.5)
division by zero doesn’t work.
addition
Print(-4+4)
Print(-5+0.2)
subtraction
subtraction operator is obviously the – (minus) sign,
although you should note that this operator also has another
meaning – it can change the sign of a number.
In subtracting applications, the minus operator expects two
arguments: the left (a minuend in arithmetical terms) and
right (a subtrahend).
Find area length of rectangle
A=5
B=6
Print(“area of the rectangle”, a*b)
or
C= a*b
Print(“area of the rectangle”, c)
o/p: 30
exercise
Write a Python program which accepts the radius of a circle
from the user and compute the area. R= 5
Find the output
lotsofhellos = "hello" * 10
print(lotsofhellos)