In [1]:
print("hello world")
In [2]:
1+2
Out[2]:
In [3]:
2*4
Out[3]:
In [4]:
5-6
Out[4]:
In [5]:
4-2
Out[5]:
In [6]:
6/3
Out[6]:
In [7]:
6//2
Out[7]:
In [8]:
5/2
Out[8]:
In [9]:
5//2
Out[9]:
In [10]:
6%3
Out[10]:
In [11]:
4**2
Out[11]:
In [12]:
5//2.0
Out[12]:
In [13]:
5.0/2
Out[13]:
In [14]:
'this is a string'
Out[14]:
In [15]:
"this is also a string"
Out[15]:
In [16]:
"'this is triple quoted string'"
Out[16]:
In [17]:
"'this
is''"
In [18]:
"""this
is"""
Out[18]:
In [19]:
"'this
is"
In [20]:
true
In [21]:
True
Out[21]:
In [22]:
False
Out[22]:
In [24]:
None
In [25]:
'string' +'added'
Out[25]:
In [26]:
'1'+'1'
Out[26]:
In [27]:
'1'+1
In [28]:
a=2
In [29]:
'a'*4
Out[29]:
In [30]:
'string'-'str'
h/w try to use all the math operators on different types of string/int/boolean/float
In [31]:
1+2j
Out[31]:
2+4j-5+6j
In [32]:
(2+4j)+(3+3j)
Out[32]:
In [33]:
2+4j-5+2j
Out[33]:
In [34]:
conditionals
VARIABLES
In [ ]:
name_of _variable = 23
In [ ]:
'NAMING OF A VARIABLE''a-zA-Z_0-9'
In [ ]:
leapfrog_wifi=3 'for python:snake case all small undescore'
In [ ]:
LeapFrogWifi=3"camel case"
In [ ]:
leapFrogWifi=3'capital case'
In [35]:
start, end=0, 1
In [36]:
start
Out[36]:
In [37]:
end
Out[37]:
CONDITIONALS
In [38]:
if True:
print("heyy")
In [44]:
if (start<0):
print("this is negative")
elif start==0:
print("it is zero")
else:
print("greater than zero")
print("hey there im out of block")
In [ ]:
if end=1:
print("found end")
In [45]:
if start<0 or end>1:
print("start is 0 end is 1")
else:
print("neither")
In [46]:
if start==0 and end==1:
print("and")
In [47]:
2>1
Out[47]:
In [48]:
start==1
Out[48]:
In [49]:
a=2
In [50]:
1<a<3
Out[50]:
In [51]:
1<=2
Out[51]:
In [52]:
1!=2
Out[52]:
In [53]:
1==2
Out[53]:
In [54]:
True
Out[54]:
In [55]:
not True
Out[55]:
In [56]:
a= False
In [57]:
if not a:
print("a is false")
In [58]:
if !a:
print("false")
In [60]:
if a:
print("true")
else:
print("false")
In [61]:
a=''
In [62]:
if not a:
print("string empty")
if a:
print("string is nt empty")
In [63]:
not a
Out[63]:
LOOPS
In [65]:
city= 'Kathmandu'
In [ ]:
In [66]:
for char in city:
print(char)
In [67]:
for i in range(0,len(city)):
print (city[i],i)
In [69]:
for i in range(0,9):
print(city[i],i)
In [70]:
[1,2,3]
Out[70]:
In [71]:
['chitwan','kathmandu','pokhara']
Out[71]:
LISTS
In [ ]:
In [72]:
[1,'p',2.546]
Out[72]:
In [73]:
[2,3,4,[2,3,4]]
Out[73]:
In [74]:
cities=['chitwan','pokhara','kathmandu']
In [75]:
for city in cities:
print(city)
In [76]:
if'kathmandu' in cities:
print("yes")
In [77]:
'k'in 'kathmandu'
Out[77]:
In [78]:
for char in 'kathmandu':
if char=='k':
print('true')
In [79]:
range(0,9)
Out[79]:
In [80]:
for i in range (0,9):
print(i)
In [81]:
for i in range(0,9):
if i%2 == 0:
print(i)
In [82]:
for i in range (0,9,2):
print(i)
In [84]:
for i in range (0,9,4):
print(i)
In [85]:
for i in range (1,9):
print(i)
if i %5 == 0:
break
In [86]:
for i in range (1,9):
if i%5==0:
break
print (i)
In [88]:
for i in range (1,9):
if i%5==0:
print(i)
continue
In [89]:
#it is a comment.
In [90]:
i=0
while True:
if i==5:
break
print(i)
i+=1
In [91]:
num= input ("enter a nummber")
In [92]:
num
Out[92]:
In [93]:
int (num)
Out[93]:
In [94]:
float(num)
Out[94]:
In [95]:
num +2
BUILD A GUESSING GAME
In [96]:
from random import randint
In [99]:
randint(0,99)
Out[99]:
In [ ]:
from random import randint
b= randint(1,99)
print("lets start the game!!!")
i=1
while i<4:
a= input("enter a number between 1 to 100")
c= int(a)
if c<b:
print("number is slightly less")
elif c>b:
print("number is greater")
else:
print("correct guess")
print(b)
break
i+=1
In [ ]:
from random import randint
b= randint(1,99)
print("lets start the game!!!")
i=1
while i<4:
a= input("enter a number between 1 to 100")
c= int(a)
if c==b:
print("correct guess")
print(b)
break
elif c<b:
print("number is slightly less")
else:
print("number is greater")
i+=1
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: