Vocabulary
Modulo(%) |
Finding the remainder |
Syntax |
Grammar/ Structure of language |
/ |
divide(float) |
// |
divide(integer) |
Comparing Values
True or anything |
True |
False and anything |
False |
Multiplication and Exponents
string* string |
Fail!! |
string ** string |
Fail!! |
string ** number |
Fail!! |
Random
import random
int/fp/strlist = [1,2,3,4,5,6,7,8,9,0]
random_int/fp/str = random.choice(int/fp/strlist)
print(int/fp/strlist,random_int/fp/str)
#fp = float ,str = string
fplist = [4.6,3.2,7.7,6.2]
strlist = ['uik','lok','pki','roo']
mylist = [589,56.3,'suay']
random_mylist = random.choice(mylist)
print(mylist,random_mylist) |
Palindrome function
user_input = input("write down string")
print(input)
letter_num = 0
reverse = ""
while letter_num < len(user_input):
reverse = user_input[letter_num] + reverse
letter_num = letter_num + 1
print ("Reverse: ",reverse)
if reverse == user_input:
print("This is palindrome")
else:
print("This is not a palindrome")
|
Guessing Game
import random
mylist = ['lion', 'cheetah', 'panther', 'cougar', 'leopard']
random_item = random.choice(mylist)
Chances = 5
Score = 0
while Chances > 0:
print("Words:['lion', 'cheetah', 'panther', 'cougar', 'leopard']")
user_guess = input("Guess a word: ")
if user_guess == random_item:
print("That's correct!")
Score = Score+100
print("Chances remaining",'',Chances)
random_item = random.choice(mylist)
print("Score is",'',Score)
else:
if user_guess in mylist:
print("Sorry, wrong choice!")
Chances = Chances - 1
print("Chances remaining",'',Chances)
print("Score is",'',Score)
else:
print("Sorry, that is not even in the list!")
Chances = Chances - 1
print("Chances remaining",'',Chances)
print("Score is",'', Score)
if Chances == 0:
print("Gameover",'',"The word is",'',random_item)
print("Final score is",'',Score)
|
|
|
Printing Value
print("hello", "there") #displays hello there
print("hello" + "there") #displays hellothere |
Combining Strings (Concatenation)
"hi" + "there" == "hithere"
"hi" * 5 == "hihihihihi" |
List
mylist.append(5) #add at the end of the list |
Range
for num in range(100):
print (num) # prints all numbers from 0 – 99
for num in range(5, 50):
print(num) #prints all numbers from 5 - 49
|
in
if user_guess in mylist:
print("Sorry, tht's wrong")
else:
print("Sorry It is not in choice")
|
Comment
# comment
"""
Double quote
for 2 line comment
"""
''' Single quote - Multi-line comment, ''' |
Find the area of prism
def volumeofprism(b,h,l):
volume = areaofTriangle(b,h) * l
return volume
user_length = float(input('Enter the length of the prism: '))
print ("The volume of the prism is", volumeofprism(user_base,user_height,user_length))
|
Print even number
num = 2
while 1 < num and num <101 :
print(num)
num = num + 2
|
Function Largest Value
def max3 (num1,num2,num3):
if num1>num2 and num1>num3:
largestvalue = num1
elif num2>num3 and num2>num1:
largestvalue = num2
else:
largestvalue = num3
return largestvalue
print (max3(9,100,25))
print (max3(69,85,1))
print (max3(75,9,33))
def maxlist (list):
largestvalue = list [0]
for item in list:
if item > largestvalue:
largestvalue = item
return largestvalue
mylist = [1,2,3,4,103,100,89,57]
print (maxlist(mylist))
|
Palindrome2
def isPalindrome(word):
letter_num = 0
reverse = ""
while letter_num < len(user_word):
reverse = user_word[letter_num] + reverse
letter_num = letter_num + 1
if word == reverse:
return True
else:
return False
while True:
user_word = input("Enter the word")
word = len(user_word)
print(word)
if user_word == "quit":
break
if isPalindrome(user_word):
print(user_word, "is a palindrome")
else:
print(user_word, "is not a palindrome")
|
|
|
Capital letter
name = "tim GIRARD"
print (name.upper()) → TIM GIRARD
print (name.lower()) → tim girard
print (name.capitalize()) → Tim girard
print (name.title()) → Tim Girard |
Naming Conventions
Rule for giving name
- letter
- numbers
- underscore _
Valid name
- _myStr
- my3
- Hello_there
Invalid name
- 3my=”hi” -- cannot start with number
- first name=”hi”
- first-name
- first+name |
Countdown Number
user_number = input("Please enter the number")
number = int(user_number)
countdown_string = ""
while number>0:
countdown_string = countdown_string + str(number)
number = number - 1
print (countdown_string)
|
Reverse Number
word = input("enter the word")
letter_num = 0
reverse = ""
while letter_num < len(word):
reverse = word[letter_num] + reverse
letter_num = letter_num + 1
print ("Reverse: ",reverse)
|
Convert to Binary String
user_number = input ("Please enter the number")
number = int(user_number)
binary_string = ""
while (number>0):
remainder = number%2
binary_string = str(remainder) + (binary_string)
number = number//2
print ("Binary string is", binary_string)
|
Sort word per line
mystr = "Hello"
letter_num = 0
while letter_num < len(mystr):
print (mystr[letter_num])
letter_num = letter_num + 1
H
e
l
l
o
|
Define: function area of circle
def areaofcircle (r):
if r <= 0:
return "Error: Invalid radius"
pi = 3.1415
area = pi*r**2
return area
user_radius = input ('Enter the radius')
r = float(user_radius)
print("The area of circle is",areaofcircle(r))
|
Definition
def printdefinitions(word):
if word == "Variable":
print ('lo')
elif word == "Function":
print ('fun')
elif word == "Parameter" or word == "Argument":
print ('para')
elif word == "Function call":
print ('call')
elif word == "String":
print ("stri")
else:
print ("Unknown Word")
return
while True:
user_input = input ("Enter the word:")
printdefinitions(user_input)
|
|
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment