def square(x): # x is referred to as a parameter
return x * x
square(5) # 5 is referred to as an argument or input
square(10)
def printHello(name):
print("Hello, "+name)
return "I just printed something"
printHello("Raj")
printHello("Bello")
def sum(x,y):
return x+y
sum(10,20)
sum(100,200)
xs = [10,5,20,30,6,90]
index = 0
found = False
while not found and index < len(xs):
if xs[index] == 5:
found = True
else:
index = index + 1
if found:
print("I found the 5")
else:
print("I did not find the 5")
xs = (1,"raj",3)
xs[0]
xs[1]
xs[2]
## Bulls and Cows
3612
# This function takes as input a 4-digit number and
# returns a tuple of its 4 digits.
def extractDigits(num):
d4 = num%10
d3 = (num//10)%10
d2 = (num//100)%10
d1 = (num//1000)%10
return (d1,d2,d3,d4)
extractDigits(3612)
extractDigits(3490)
from random import *
randint(1000,9999)
s1 = "This is a sentence"
t1 = s1.upper() # upper is a method
print(t1)
class Student:
# CONSTRUCTOR method MANDATORY
def __init__(self,name): # need to assign a value to every attribute
self._name = name
self._courses = []
def add_course(self,cno,credits,grade):
self._courses.append((cno,credits,grade))
def gpa(self):
grade_points = {'A': 4, 'B': 3, 'C': 2, 'D': 1, 'F': 0}
points = 0
total_credits = 0
for (cn,cr,g) in self._courses:
points = points + cr*grade_points[g]
total_credits = total_credits + cr
if (total_credits > 0):
return round(points/total_credits,2)
else:
return None
s1 = Student("Raj")
s1._name
s1._courses
s1.add_course('101',3,'A')
s1.add_course('102',3,'A')
s1.add_course('201',3,'B')
s1.add_course('202',3,'C')
s1._name
s1._courses
s1.gpa()