Wednesday, August 22, 2018

A/L Python in one hour - Learn by example



Variables

x=3
y="Nimal"
z=True
r=3
2x=5
_r=45
a$5=3

Operators
= + - / *
%  //
**
5 + 3 * 2
5 + 3 * 2 / 2

#single line comment

"""
multi line
comment
"""

'''
multi
line
comment
'''

Input
name = input("Enter Your Name ")
print (name)

Data Types : number, string, boolean, list, dict
x+y
r+x

Data Type Conversion

nt=input("n tofees ")
p=input("person ")
s=p + " got " + nt + " tofees"
print(s)


x=input()
x=int(input())

nt=int(input("n tofees"))
pt=int(input("pr toffes"))
tc=nt * pt
print (tc)

nt=input("n tofees")
pt=input("pr toffes")
tc=int(nt) * int(pt)
print(tc)

nt=7
p=input("person ")
s=p + " got " + str(nt) + " tofees"
print(s)


Comparison
==
!=
===
>=
<=

IF
x=11
if x < 10:
    print("low")
elif x == 10:
    print ("equal")
else:
    print("high")

While
x = 1
while x < 5:  # condition for loop
    print (x)
    x = x + 1
    print ('I love python')

print ('done')

For
#0 to 9
for i in range (10): 
    print (i)


#range (5,10) is 5 through 9
for c in range (5,10):
    print (c)

for c in range (1,6):
    if c == 3:
        break
    print (c)
 
print ("out")


for letter in 'Python':   
   if letter == 'h':
      continue
   print ('Current Letter :', letter)

for i in range(4):  #to iterate between 10 to 20
    for j in range(4):
        print (i, j, i * j)


Functions

def greeting(name):
    print ("Hello " + name)

greeting("A")
greeting("B")
greeting("C")

def add(a,b ):
   c=a+b
   return c #  return value

d=add(1,3)
print (d)
e=add(d,3)
print(e)
f= add(d,e)
print(f)

Formatting
# %d means integer %s is string
x = 2
y = 7
print ("-old-")
print ('The sum of %d and %d is %d' % (x, y, x + y))
print ('The sum of %d and %d is %d is %s' % (x, y, x + y, "OK"))

print ("-New-")
print ('The sum of {} and {} is {}'.format (x, y, x + y))
print ('The multi of [{}] and *{}* is {}'.format (x, y, x * y))

List
list=[1,2,3,4,5,6]

for x in list:
    print (x)

list=[]
print (list)
for i in range(5):
    list.append(i)

print (list)

stack= []
stack.append(1)
stack.append("Sri Lanka")
stack.append( 4)
stack.append( True)
print(stack)

print(len(stack))

a=stack.pop()
print(a)
print(stack)
stack.pop()
print(stack)

Array & List
Array=(3,2,1)
List=[3,2,1]

print(List, Array)
print(Array[0],List[0])

List[2]=4
print(List)

# Array[2]=4 # error

dict = {'Name': 'Ganesh', 'Age': 17, 'Class': 12}
print(dict)
print (dict['Name'])
print (dict['Age'])


dict['Age'] = 18
dict['Class'] = 13
dict['School'] = "RRCK"

print(dict)

Files
f = open("myfile.txt", "w") #write only
f.write( "Python is cool\n")
f.write( "iPython is very cool")
f.close() #Close file


f = open("myfile.txt", "r")
s = f.read()
print(s)
f.close()

input()

f = open("myfile.txt", "r")
line = f.readline()
print(line)
f.close()

input()

f = open("myfile.txt", "r")
lines = f.readlines()
print(lines)
f.close()

List Addressing
L=[0,1,2,3,4,5,6]
print(L)
print("L[0]" , L[0])
print("L[2:]", L[2:])
print("L[:3]", L[:3])
print("L[2:3]", L[2:3])
print("L[:]", L[:])
print("L[:0]",L[:-1])
print("L[:-2]", L[:-2])


1. Study and Run Codes
2. Explore what happens - do some tinkering
3. Draw Flow Charts

Solve this problem

  1. Once upon a time, there were three little crows - ages 2, 4, and 6. What is the total of their ages?

  1. Each little Crow wanted to build a house. Crow #1 wanted to build a house of straw. Straw costs Rs.4 a bundle. He needs 9 bundles. How much will he spend?
  2. Once upon a time, there were three little crows. They were Lady Crow, Lord Crow, and Sister Crow. Lady Crow was 5 years old. Lord Crow was 4 years old and Sister Crow was 3 years old. What was their total age?
4. Master Crow bought 4 milk toffees at 3 Rupees. Next he bought 3 chocs, each cost 10 Rupees. How much balance he would get when he give 100 Rupee note.

5. Your Grandpa likes you talking with him. If you type in normal case, he responds with "HUH?! SPEAK LOUD KID!" . If you type in capital, he responds with "NO, NOT SINCE 1945!". Every time he responds he forget the year and tell a random year between 1945 to 1960.


When you say "BYE" he says "STAY!". But when you say "BYE" 3 times you can leave.



No comments:

Post a Comment