In [3]:
var = input("Enter a number ")
Enter a number 1
In [4]:
type(var)
Out[4]:
str
input()      --> str()
Type casting --> int(), float(), complex()
eval(input())--> list(), tuple(), dic(), set()
In [21]:
var = eval(input("Enter data "))
Enter data "Ram"
In [22]:
type(var)
Out[22]:
str
In [23]:
var
Out[23]:
'Ram'
In [24]:
a = 10
print(a)
10
In [27]:
def function(a):
    return a, type(a)
In [28]:
function(10)
Out[28]:
(10, int)
In [29]:
function("hello")
Out[29]:
('hello', str)
In [30]:
function([23,41,234,132,423,4])
Out[30]:
([23, 41, 234, 132, 423, 4], list)
In [38]:
def results(marks):
    return max(marks), min(marks), sum(marks), sum(marks)/len(marks)
In [39]:
results([34, 56, 78, 56, 89])
Out[39]:
(89, 34, 313, 62.6)
def func(known_variables):
    ...
    return unknown_value
func(known_values)
In [41]:
def addtwo(a, b):           # input
    c = a+b                 # processing
    return c                # output
    
print(addtwo(23523452345, 523452345234523452345))
523452345258046904690

Data types

  • Number - int(), float(), complex()
  • String - str()
  • List - list()
  • Tuple - tuple()
  • Dictionary - dict()
  • Set - set()
In [44]:
l = [1, 2, 3, 4]
In [45]:
l.append(5)
In [46]:
l
Out[46]:
[1, 2, 3, 4, 5]
In [47]:
l = (1, 2, 3, 4)
In [48]:
l.append(5)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-48-129491e5efc7> in <module>()
----> 1 l.append(5)

AttributeError: 'tuple' object has no attribute 'append'
In [49]:
#     0    1    2
l = ['a', 'b', 'c']
In [50]:
l[0]
Out[50]:
'a'
In [51]:
l.pop(0)
Out[51]:
'a'
In [52]:
l
Out[52]:
['b', 'c']
In [53]:
l[0]
Out[53]:
'b'
In [58]:
l = {10:'a', 'bat':'b', 12:'c'}
In [59]:
type(l)
Out[59]:
dict
In [60]:
l = {10, 'bat', 12}
In [61]:
type(l)
Out[61]:
set
In [62]:
l = [2, 1, 2, 3, 2, 1]
In [63]:
set(l)
Out[63]:
{1, 2, 3}

WAP to convert temp

f = c*9/5+32
In [80]:
def convert_temp(celc):
    return "%.2f" % (celc*9/5+32)
In [81]:
convert_temp(29)
Out[81]:
'84.20'
In [82]:
convert_temp(22)
Out[82]:
'71.60'
In [83]:
convert_temp(22.78)
Out[83]:
'73.00'
In [79]:
"%.2f" %(10/3)
Out[79]:
'3.33'
In [84]:
10/3
Out[84]:
3.3333333333333335
In [85]:
10//3
Out[85]:
3
In [86]:
"%.2f" % 10.456
Out[86]:
'10.46'

WAP to calculate BMI

BMI = weight/height*height

In [89]:
def bmi_calc(w, h):
    return "%.1f" % (w/h**2)
In [90]:
bmi_calc(89, 1.7)
Out[90]:
'30.8'

Pythagorous Theorem

In [99]:
import math
def pytha(base, height):
    return "%.1f" % math.sqrt(base**2 + height**2)
In [100]:
pytha(3, 4)
Out[100]:
'5.0'
In [101]:
pytha(32, 45.6)
Out[101]:
'55.7'

Operators

Arithmatic operators

+ - * / 
// : floor division
% : modulus
** : exponent

2) 11( 5
   10
   ---
    1

Relational operators - Boolean

> < == != >= <=

Containment operators

in 
not in

Others

not 
and
or
+= -=

Operator precedance

()
**
* / // %
+ -
L -> R
In [103]:
11/2
Out[103]:
5.5
In [105]:
11//2
Out[105]:
5
In [106]:
11%2
Out[106]:
1
In [107]:
11//2
Out[107]:
5
In [108]:
11/2
Out[108]:
5.5
In [109]:
'h' in "hello"
Out[109]:
True
In [110]:
'h' in "aeiou"
Out[110]:
False
In [111]:
5 in [4, 5, 2]
Out[111]:
True
In [112]:
5 not in [4, 5, 2]
Out[112]:
False

Equilateral triangle

In [120]:
def is_equi(a, b, c):
    return a==b==c and a>0
In [121]:
is_equi(-2, -2, -2)
Out[121]:
False
In [122]:
is_equi(3, 2, 3)
Out[122]:
False
In [123]:
is_equi(1, 1, 1)
Out[123]:
True

Isosceles triangle

In [124]:
def is_iso(a, b, c):
    return a>0 and b>0 and c>0 and (a==b or b==c or c==a)
In [126]:
is_iso(2, 3, 2)
Out[126]:
True

Divisibility by both 7 or 11

In [135]:
def check_div(num):
    return num%7==0 or num%11==0
In [136]:
check_div(14)
Out[136]:
True
In [137]:
check_div(77)
Out[137]:
True
In [138]:
check_div(22)
Out[138]:
True
In [139]:
check_div(13)
Out[139]:
False
In [140]:
2+2*3
Out[140]:
8
In [141]:
(2+2)*3
Out[141]:
12
In [142]:
2*3**2
Out[142]:
18
In [151]:
count = 0
word = input("Enter a word ")
for i in word:
    if i.lower() in "aeiou":
        count += 1
print(count)
Enter a word India
3
In [154]:
var = ""
word = input("Enter a word ")
for i in word:
    if i.lower() in "aeiou":
        var += i
print(var)
Enter a word India
Iia
In [155]:
## Distance b/w points
import math
def distance(x1, x2, y1, y2):
    return math.sqrt((x2-x1)**2 + (y2-y1)**2)
In [156]:
distance(4, 12, 8, 14)
Out[156]:
10.0
In [157]:
'lower' in dir(str)
Out[157]:
True
In [158]:
'lower' in dir(list)
Out[158]:
False
In [163]:
if 1==2:
    print("hello")
print("Thank you")
Thank you
In [165]:
import keyword
'case' in keyword.kwlist
Out[165]:
False
In [ ]: