This Is Python Tutorial: Print
This Is Python Tutorial: Print
Hellow World
a = b + c
Variables
In [1]: x = 3
In [2]: %whos
In [3]: print(type(x))
<class 'int'>
In [4]: x = 5.7
In [5]: %whos
In [6]: print(type(x))
<class 'float'>
In [8]: %whos
In [12]: %whos
In [13]: print(abcd)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-13-ce1394b9d4d9> in <module>
----> 1 print(abcd)
In [14]: c = 2+4j
In [15]: print(type(c))
<class 'complex'>
In [17]: print(type(s))
<class 'str'>
Operators
In [18]: %whos
In [20]: print(sumOfaAndb)
In [21]: type(sumOfaAndb)
Out[21]: int
In [22]: type(a+d)
Out[22]: float
In [23]: v = ((a+d)**3)/4
In [24]: print(v)
265.30199999999996
In [25]: s1 = "hellow"
s2 = "world"
s = s1+s2
print(s)
hellowworld
In [26]: 10//3
Out[26]: 3
In [27]: 10/3
Out[27]: 3.3333333333333335
In [28]: _
Out[28]: 3.3333333333333335
In [29]: 3x = 5
In [31]: *t=4
In [32]: _e = 6
In [34]: %whos
Bool
In [35]: a = True
b = True
c = False
In [36]: %whos
True
False
False
In [38]: d = a or c
print(d)
True
In [39]: not(a)
Out[39]: False
In [40]: not(b)
Out[40]: False
In [41]: not(c)
Out[41]: True
In [42]: t = not(d)
In [43]: type(t)
Out[43]: bool
In [44]: print(t)
False
Out[45]: False
Comparisons
In [46]: print(2<3)
True
In [47]: c = 2<3
print(type(c))
print(c)
<class 'bool'>
True
In [48]: d = 3==4
In [49]: print(d)
False
In [50]: 3==3.0
Out[50]: True
In [51]: x = 4
y = 9
z = 8.3
r = -3
Out[52]: True
Out[53]: False
Out[57]: False
False
In [60]: print(round(4.556))
In [61]: print(round(4.345))
In [64]: print(round(4.556389,3))
4.556
In [66]: B = divmod(27,5)
In [67]: type(B)
Out[67]: tuple
In [68]: print(B)
(5, 2)
In [69]: B[0]
Out[69]: 5
In [70]: B[1]
Out[70]: 2
In [71]: divmod(22,10)
Out[71]: (2, 2)
In [73]: G = divmod(34,9)
In [74]: type(G)
Out[74]: tuple
In [75]: print(G)
(3, 7)
In [76]: G[0]
Out[76]: 3
In [77]: G[1]
Out[77]: 7
In [78]: 34//9
Out[78]: 3
In [79]: 34%9
Out[79]: 7
In [80]: isinstance(3,int)
Out[80]: True
In [83]: isinstance(3.4,(float,int))
Out[83]: True
In [86]: isinstance(2+3j,(int,float,str,complex))
Out[86]: True
In [87]: pow(2,4)
Out[87]: 16
In [88]: 2**4
Out[88]: 16
In [89]: pow(2,4,7)
Out[89]: 2
Out[91]: str
In [92]: x = int(x)
In [93]: type(x)
Out[93]: int
In [94]: print(x-34)
22
In [96]: type(a)
Out[96]: float
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-97-5ddd2644c78a> in <module>
----> 1 b = float(input("Enter a real number : "))
In [100]: help(pow)
pow(x, y, z=None, /)
Equivalent to x**y (with two arguments) or x**y % z (with three arguments)
Some types, such as ints, are able to use a more efficient algorithm when
invoked using the three argument form.
In [101]: help(input)
Raises
------
StdinNotImplentedError if active frontend doesn't support stdin.
In [104]: a = int(input())
b = int(input())
if a>b:
print(a)
print("I am still inside if condition")
print("I am outside the if condition")
10
45
I am outside the if condition
In [106]: a = int(input())
b = int(input())
if a>b:
print(a)
if b>a:
print(b)
22
4
22
In [109]: a = int(input())
b = int(input())
if a>b:
print(a)
print("if part")
else:
print(b)
print("else part")
10
10
10
else part
In [112]: a = 10
b = 10
if a==b:
print("Equal")
elif a>b:
print("A")
else:
print("B")
print("Not in if")
Equal
Not in if
In [114]: a = int(input("Enter Marks :"))
if a >= 85:
print("A Grade")
elif (a < 85) and (a >= 80):
print("A- Grade")
elif a < 80 and a >= 75:
print("B Grade")
elif a < 75 and a >= 70:
print("B- Grade")
else:
print("Below Average")
In [116]: a = 13
if a>10:
print(">10")
elif not(a>10):
print("Else part")
>10
In [118]: a = int(input())
if a>10:
print(">10")
print("Inside the top if")
if a>20:
print(">20")
print("Inside the nested if")
if a>30:
print(">30")
print("inside the nested if of nested if")
else:
print("<=30")
print("inside the else part of nested if of nested if")
else:
print("<=20")
print("Inside the else part of nested if")
print("Outside all ifs")
25
>10
Inside the top if
>20
Inside the nested if
<=30
inside the else part of nested if of nested if
Outside all ifs
In [132]: # single line comment
"""
User will enter a floating point number let say 238.915. Your task
is to find out the integer portion before the point (in this case 238)
and then check if that integer portion is an even number or not?
"""
x = float(input("Enter a real number :"))
y = round(x)
if x>0:
if y>x:
intPortion = y-1 # 29.6
else:
intPortion = y
else:
if y<x:
intPortion = y+1
else:
intPortion = y
if intPortion%2 == 0:
print("Even")
else:
print("Odd")
In [125]: round(-9.3)
Out[125]: -9
In [126]: round(-9.6)
Out[126]: -10
In [133]: n = int(input())
i = 1
while i < n:
print(i**2)
print("This is iteration number :", i)
i+=1 # i = i+1
print("Loop done")
5
1
This is iteration number : 1
4
This is iteration number : 2
9
This is iteration number : 3
16
This is iteration number : 4
Loop done
In [135]: n = 10
i = 1
while True:
if i%9 == 0:
print("Inside if")
break
else:
print("inside Else")
i = i+1 # i+=1
print("done")
inside Else
inside Else
inside Else
inside Else
inside Else
inside Else
inside Else
inside Else
Inside if
done
In [136]: n = 10
i = 1
while True:
if i%9 != 0:
print("inside if")
i +=1
continue
print("something")
print("somethingelse")
break
print("done")
inside if
inside if
inside if
inside if
inside if
inside if
inside if
inside if
something
somethingelse
done
In [140]: L = []
for i in range(1,20,3):
print(i)
L.append(i**2)
print(L)
1
4
7
10
13
16
19
[1, 16, 49, 100, 169, 256, 361]
In [142]: S = {"apple",4.9,"cherry"}
i = 1
for x in S:
print(x)
i+=1
if i==3:
break
else:
pass
else:
print("Loop terminates with success")
print("Out side the loop")
apple
4.9
Out side the loop
In [143]: D = {"A":10,"B":-19,"C":"abc"}
for x in D:
print(x,D[x])
A 10
B -19
C abc
In [149]: """ Given a list of numbers i.e. [1,2,4,-5,7,9,3,2], make another list
that contains all the items in sorted order from min to max. i.e. your
result will be another list like [-5,1,2,2,3,7,9]
"""
L = [1,2,4,-5,7,9,3,2]
for j in range(len(L)):
m = L[j]
idx = j
c = j
for i in range(j,len(L)):
if L[i]<m:
m = L[i]
idx = c
c+=1
tmp = L[j]
L[j] = m
L[idx] = tmp
print(L)
[-5, 1, 2, 2, 3, 4, 7, 9]
In [147]: L
Out[147]: [-5, 2, 4, 1, 7, 9, 3, 2]
In [2]: printSuccess()
I am done
send me another task
In [3]: 3+8
Out[3]: 11
In [4]: printSuccess()
I am done
send me another task
In [10]: help(printSuccess2)
printSuccess2()
This function is doing nothing except printing a message.
That message is "hellow"
In [11]: printSuccess2()
hellow
if isinstance(msg,str):
print(msg)
else:
print("Your input argument is not a string")
print("Here is the type of what you have supplied :",type(msg))
In [13]: help(printMsg)
printMsg(msg)
The function prints the message supplied by the user
or prints that msg is not in the form of string
In [15]: printMsg?
In [17]: printMsg(23)
hellow there
In [20]: mypow?
In [21]: mypow(3,4)
81
In [23]: checkArgs(3,4,5)
144
In [24]: checkArgs(3,4,"g")
In [25]: checkArgs(3,4)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-25-f6f193ef9e63> in <module>
----> 1 checkArgs(3,4)
In [26]: checkArgs(2,3,4,5)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-26-182e860594b3> in <module>
----> 1 checkArgs(2,3,4,5)
A is : 3
B is : game
C is : 2
A is : 2
B is : 3
C is : game
In [37]: d = myadd(2,3)
print(d)
In [38]: variableOutSideTheFunction = 3
In [47]: print(type(g()))
<class 'NoneType'>
In [43]: print(variableOutSideTheFunction)
In [53]: print(type(h()))
A
something
<class 'int'>
5 7 something
In [58]: print(myAddUniversal(2,4,5,4.6,78))
93.6
In [62]: gg()
In [63]: gg(56)
56
In [64]: L = [1,2,3]
L2 = L
L2[0] = -9
print(L)
[-9, 2, 3]
In [66]: L2 = [12,3,4]
ff()
1
2
In [67]: ff(L2)
12
3
4
In [68]: ff()
1
2
In [76]: d = addAllNumerics(2,3,4,5,6,7)
print(d)
27
In [73]: c = myfs.addAllNumerics(2,3,4,6)
In [74]: print(c)
15
In [78]: myfs.myName
In [79]: """ Given a list of numbers i.e. [1,2,4,-5,7,9,3,2], make another list
that contains all the items in sorted order from min to max. i.e. your
result will be another list like [-5,1,2,2,3,7,9]
"""
Out[79]: ' Given a list of numbers i.e. [1,2,4,-5,7,9,3,2], make another list\nthat contains all
the items in sorted order from min to max. i.e. your \nresult will be another list like
[-5,1,2,2,3,7,9]\n'
In [84]: print(a,b)
0 3
In [85]: def swapValues(L,idx1,idx2):
tmp = L[idx1]
L[idx1] = L[idx2]
L[idx2] = tmp
return L
In [86]: L = [2,3,6,7]
L2 = swapValues(L,1,3)
print(L2)
[2, 7, 6, 3]
In [103]: L2 = sortList([2,1,5,3,-8,17])
print(L2)
[-8, 1, 2, 3, 5, 17]
In [91]: checkIfNotNumeric??
In [99]: print(checkIfNotNumeric2([2,1,5,3,8,17]))
True
In [109]: type(s)
Out[109]: str
In [110]: print(s)
In [117]: price = 12
s = "The price of this book"
v = s + ' is: ' + str(price)
print(v)
print(s,"is:",price)
this is line 1
this is line 2
this is last line and this line is 3
In [122]: type(s[5])
Out[122]: str
In [125]: s[0:10]
In [126]: s[-1]
Out[126]: 'u'
In [127]: s[-3]
Out[127]: 'y'
In [129]: s[-12:-3]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-130-6ca1c1f7962b> in <module>
----> 1 s[1] = "e"
In [131]: s[0:12:2]
Out[131]: 'Hwaeyu'
In [132]: s
In [133]: #s[start:end:step]
s[:12]
In [134]: s[3:]
In [135]: s[1:12]
In [136]: s[::-1]
In [139]: c = a.upper()
print(c)
In [140]: d = a.replace(";","*")
print(d)
In [143]: a = "abc;def;hgydfa;yy23"
L = a.split(";")
print(L)
In [144]: L[1]
Out[144]: 'def'
In [147]: print(a.capitalize())
Abc;def;hgydfa;yy23
In [149]: "abdAfadfGGQ".capitalize()
Out[149]: 'Abdafadfggq'
In [150]: help(a.count)
Out[153]: True
Out[154]: True
In [156]: "abcdefghi"<"def"
Out[156]: True
In [157]: "$%"<"*&"
Out[157]: True
In [158]: print("we are learning "string" here")
In [163]: print("c:\name\drive")
c:
ame\drive
In [164]: print(r"c:\name\drive")
c:\name\drive
In [ ]: