Expressions and Data types like – integer, float, text etc.
simple expressions like –
5 + 5
10 – 5
Practice –
a = 5
print(a)
b = 10
c = a + b
print("value of a is",a)
print("value of b is",b)
print("a + b = ",c)
Another example of the variables …
a = 5
b = 2
c = 8
d = 9
e = a + b + c + d
print("the value in variable a is", a)
print("the value in variable b is", b)
print("the value in variable c is", c)
print("the value in variable d is", d)
print("addition result of a + b + c + d is ..... ", e)
one more example
a=900
b=10000
c=110000000
f=a*b*c
print("First number is.....", a)
print("Second mumber is.....", b)
print("Third mumber is.....", c)
print("multiplication of a b and c is ......", f)
a=900
b=10000
c=110000000
f=a*b*c
print("First number is.....", a)
print("Second number is.....", b)
print("Third number is.....", c)
print("multiplication of a b and c is ......", f)
4 basic mathencal operators –
Plus +
Minus -
Multiply *
Division /
Float Division operator
Float divion operator //
this operator only show the result by getting the interger part and do not take fraction part, it ignore the fractional part
Modulus Operator, it show the reminnder after division
Moduleus %
we can use this operator to check is number if odd or even
Power Operator **, it is bery userful in mathematical operations, like raise to power functions
power - **
eg. 2 ** 4 i.e. 2 raise to power 4
Negation operator – this is useful to convert the positve to negative number and vice-versa
eg...
>>> a = 5
>>> -a
-5
OPERATOR’S ORDER OR PRIORITIES OR PRECEDENCE
priority for the operators begins -
1 - parenthesis
2 - exponent
3 - multiply
4 - divide
5 - add
6 - subtract
eg---
>>> a+b*c/d-a
4.5
>>> b*c/d
4.5
>>> b*c/d-a
-0.5
>>> b*c/d-a+a
4.5
>>>
NOTE: () parenthesis has always first priority
integers and numbers can be added but strings cannot be subtracted
we can multiply strings however
like:
"TechSteppers" * 2
example below
"Ganesh" * 10
'GaneshGaneshGaneshGaneshGaneshGaneshGaneshGaneshGaneshGanesh'
Place holders in for the sentences
remark = "%s is a beautiful place"
remark
'%s is a beautiful place'
remark%name
Traceback (most recent call last):
File "", line 1, in
NameError: name 'name' is not defined
remark%place
'shimla is a beautiful place'
remark = "%s and %s are beaultiful places"
place1 = shimla
Traceback (most recent call last):
File "", line 1, in
NameError: name 'shimla' is not defined
place1 = "shimla"
place2 = "Manali"
remark$place1$place2
File "", line 1
remark$place1$place2
^
SyntaxError: invalid syntax
remark
'%s and %s are beaultiful places'
remark%("Shimla","Manali")
'Shimla and Manali are beaultiful places'
distance = "%s is %d kms away from Chandigarh"
distance%("Manali", 250)
'Manali is 250 kms away from Chandigarh'
distance = "%s is %d kms away from %s"
distance%("Manali", 250, "Chandigarh")
'Manali is 250 kms away from Chandigarh'
Lists in Python
fruits["apple", "bannana", "pears", "guava"]
Traceback (most recent call last):
File "", line 1, in
NameError: name 'fruits' is not defined
fruits=["apple", "bannana", "pears", "guava"]
fruits[3]
'guava'
fruits[1]
'bannana'
fruits[0]
'apple'
fruits[0:2]
['apple', 'bannana']
fruits[0:4]
['apple', 'bannana', 'pears', 'guava']
fruits[0:5]
['apple', 'bannana', 'pears', 'guava']
fruits[4] = "greps"
Traceback (most recent call last):
File "", line 1, in
IndexError: list assignment index out of range
fruits[3] = "greps"
fruits.append("guava")
fruits[0:5]
['apple', 'bannana', 'pears', 'greps', 'guava']
fruits.append("cherry")
fruits[0:5]
['apple', 'bannana', 'pears', 'greps', 'guava']
fruits[6]
Traceback (most recent call last):
File "", line 1, in
IndexError: list index out of range
fruits[5]
'cherry'
fruits[0:6]
['apple', 'bannana', 'pears', 'greps', 'guava', 'cherry']
del fruits[2]
fruit[0:5]
Traceback (most recent call last):
File "", line 1, in
NameError: name 'fruit' is not defined
fruits[0:5]
['apple', 'bannana', 'greps', 'guava', 'cherry']
fruits.append("pears")
fruits[0:5]
['apple', 'bannana', 'greps', 'guava', 'cherry']
numb1 = (1, 2, 4, 5, 8, 20)
numb1
(1, 2, 4, 5, 8, 20)
max.numb1
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'builtin_function_or_method' object has no attribute 'numb1'
fruits * 2
['apple', 'bannana', 'greps', 'guava', 'cherry', 'pears', 'apple', 'bannana', 'greps', 'guava', 'cherry', 'pears']
max(numb1)
20
min(numb1)
1
Dictionaries in the Pyhton-
dictionary has a key with the value, if we want to access the key we get a value out of it
for example –
students and their age, students name is a key and age is the value –