function
def nameOfFunction():
print (‘This function has no parameters’)
print (‘This function has no return value’)
return # no value, just exits the function
#function call
nameOfFunction()
#function with 1 parameter/argument
def testFunction(param):
print (‘This function has 1 parameter’)
print (param)
#function call
testFunction (“this is the parameter value”)
#function with 2 parameters and a return value
def function3(param1, param2):
print(‘This function has 2 parameters’)
return param1 + param2 # return value
#function call and store the result in a variable
returnValue = function3(2, 3)
print (returnValue) |
Countdown
user_number = input("What is the number?" )
number = int(user_number)
countdown_string =' '
while number > 0:
countdown_string = countdown_string + str(number)
number = number-1
print(countdown_string) |
Countdown
user_number = input("What is the number?" )
number = int(user_number)
countdown_string =' '
while number > 0:
countdown_string = countdown_string + str(number)
number = number-1
print(countdown_string) |
one word per line
mystr = input(" Please enter your word")
letter_num = 0
while letter_num < len(mystr):
print (mystr[letter_num])
letter_num = letter_num + 1 |
one word per line
mystr = input(" Please enter your word")
letter_num = 0
while letter_num < len(mystr):
print (mystr[letter_num])
letter_num = letter_num + 1 |
Function of name
mystr = "hello THERE"
print (mystr.upper()) > HELLO THERE
print (mystr.lower()) > hello there
print (mystr.capitalize()) > Hello there
print (mystr.title()) > Hello There |
asking name
firstname = input("What is your fisrt name?")
lastname = input("What is your last name?")
fullname = firstname + " " + lastname
print(fullname)
letternumber = input("What is the letter of number?")
letternumber = int(letternumber)
if letternumber>len(fullname):
print("Invalid letter number, try again")
else:
print(fullname[letternumber])
times = input("How many times to print the letter?")
times = int(times)
if times>100:
print("Too many letters to print")
else:
print(fullname[letternumber]*times)
Result
What is your fisrt name? Pear
What is your last name? Tan
Pear Tan
What is the letter of number? 4
r
How many times to print the letter? 12
rrrrrrrrrrrr |
List
myself= "hello123"
numbers = [1,2,3,4,5,6]
print(numbers)
shoppinglist = ['shoe','bags','pants','shirt']
print(shoppinglist)
mixed=[1,'Hello',2.5, True, False]
print(mixed)
letter_num = 0
while letter_num < len(mystr):
print (mystr[letter_num])
letter_num = letter_num + 1
for myletterisawesome in mystr:
print(myletterisawesome)
for tientien in shoppinglist:
print(opal)
shoppinglist. append('ties')
print(shoppinglist)
out = 0
for mrtim in shoppinglist:
out=out + 1
print(mrtim)
print (out)
largelist = range(100)
for num in largelist:
print(num) |
Symbol n vocab
variable-A value or thing that can be changed
string-A list of character such as letter or symbol
modulo-Find the remainder
**-exponent
/-divide and quotient (result) is float
//-divide and quotient (result) is integer
!= - not equal to
<= - less than or eqaul to
>= - more than or equal to
True or anything
Always true
False and anything
False |
for loop print 0 01 012 0123 01234
mystring = ""
for num in range(5):
mystring = mystring + str(num)
print (mystering) |
|
|
print thing in list while loop
my list = [1,2,3,4,5]
num = 0
while num<len(my list):
print(mylist[num])
num=num+1 |
convert binary
user_number = input("Please enter a 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)
Result
Please enter a number 36
Binary string is 100100 |
range
#creates a list of numbers from 0 to the specified
number
numberlist = range(5)
# is the same as creating the following list
numberlist2 = [0, 1, 2, 3, 4]
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 |
palindrome
def palindrome(word):
letter_num = 0
reverse = ""
for letter_num in word:
reverse = letter_num + reverse
if word == reverse:
return True
else:
return False
while True:
user_word = input("Please enter a word: ")
if user_word != "quit":
print("This word has",len(user_word),"letters")
if user_word == "quit":
break
if palindrome(user_word) == True:
print(user_word,"is palindrome")
else:
print(user_word,"is not palindrome") |
0 01 012 0123 01234
expected out put
0 01 012 0123 01234
mystring = ""
count = 0
while count<5:
mystring = mystring+str(count)
print(mystring)
count = count+1 |
function multiplication table 5'1-5 5'2-10
def muHiplicationTable():
user_input = input("enter a number:")
num = int(user_input)
count = 1
while count <=10:
print(num,"",count,"=",numcount)
count = count + 1 |
what is the output of the following code
x = false
print (x and True or 1 == 1
ans- true
y = true
print (not y or 2<3)
and-true |
area of triangle and prism
def areaOfTriangle(b,h):
if user_base<=0:
return "Error: invalid radius"
if user_height<=0:
return "Error: invalid radius"
area = 0.5 b h
return area
user_base =float(input('Enter the base of the triangle:'))
user_height = float(input('Enter the height of the triangle: '))
print ('The area of the triangle is',areaOfTriangle(user_base,user_height))
def volumeOfPrism(b,h,l):
volume = bhl
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)) |
area of circle
def areaOfCircle (user_radius):
if user_radius<=0:
return "Error: invalid radius"
pi = 3.1415
area = pi(user_radius*2)
return area
user_radius = float(input("Enter the radius: "))
print('The area of the circle is', areaOfCircle(user_radius) |
Rules of naming var
# letters
# numbers
# underscore (_)
# can either start with letter or underscores ONLY
# no space
Example
Hello_there
me2
_mynumber
Invalid names
# 3my =cannot start with number
# last name = no spaces allowed
# last-name = dashes are not accepted |
multiplication
string * number - Repeat those thing for the number of time
string * string - Crash!
number * number - Multiply like in math
string ** number - Crash!
number ** number - Exponent in Math
number ** string - Crash!
sring + string - combine those strings together
string + number - program will be crash
|
|
|
print all even num 1-100 while loop
num = 0
while num<100
num=num+2
print(num) |
fibonacci from 0-50
num1 = 0
num2 = 0
fibonacci = num1+num2
myoutput = "0,1"
while fibonacci < 50:
myoutput = myouput + "," + str(fibonacci)
num1=num2
num2 = fibonacci
fibonacci = num1+ num2
print(my output)
0,1,1,2,3,5,8,13,... |
ask user and is num divisible by3
num = int(input(input("enter a number"))
remainder = num % 3
if remainder == 0:
print (num, "is divisible by 3")
else:
print (num, "isn't divisible by 3") |
ยากๆอะ
write a program that repeatly receive positive int from user enters a negative integer exit the loop print how many of numbers entered were even and odd
evencount=0
oddcount=0
while True:
num = int(input("enter a positive integer"))
if num<0:
print("even number:",evencount)
print("odd numbers:",oddCOunt)
break
else:
if(num%2)==0
evencount - evencount+1
else:
oddcount = oddcount + 1 |
Reverse
word = input("Please enter a word to reverse: ")
letter_num = 0
reverse = ""
while letter_num <len(word):
reverse = word[letter_num] + reverse
letter_num = letter_num + 1
print("Reverse: ",reverse) |
guess game
import random
mylist = ['lion','cheetah','panther','cougar','leopard']
random_item = random.choice(mylist)
print(random_item)
print(mylist[0])
user_guess = input("Guess a word: ")
if user_guess == random_item:
print("Correct")
else:
if user_guess in mylist:
print("Yes, it is in the list")
else:
print("No, it is not in the list") |
guess game
import random
mylist = ['lion','cheetah','panther','cougar','leopard']
random_item = random.choice(mylist)
print(random_item)
print(mylist[0])
user_guess = input("Guess a word: ")
if user_guess == random_item:
print("Correct")
else:
if user_guess in mylist:
print("Yes, it is in the list")
else:
print("No, it is not in the list") |
def of word
def printDefinition(word):
# write a definition in your own words for the folllowing words:
# use multi-line strings to print the definition
#variable
if word == "variable":
print("""A variable is thing that can be changed""")
elif word == "function":
#function
print (""" A function is a thing that reuse block or quote. """)
elif word == "parameter":
#parameter
print("""A parameter is thing inside blacket of function """)
elif word == "agument":
#argument
print(""" A argument is the same thing as parameter. It is thinfg inside blacket f function """)
elif word == "function call":
#function call
print("""Function is the thing make fuction run.""")
elif word == "string":
#string
print(""" A string is a list of character""")
else:
print("unknown word")
while True:
word = input ("Enter the word")
printDefinition(word)
Result
Enter the wordvariable
A variable is thing that can be changed
Enter the wordfunction
A function is a thing that reuse block or quote.
Enter the wordagument
A argument is the same thing as parameter. It is thinfg inside blacket f function
Enter the wordfunction call
Function is the thing make fuction run.
Enter the wordstring
A string is a list of character
Enter the wordpear
unknown word |
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
More Cheat Sheets by mmildmilds