Addition
string + string |
combine together |
string + number |
crash |
number + number |
math - addition |
Vocabulary
Variable |
Hold a value and can be change |
String |
A list of character such as number, letter, and symbols |
Integer number |
Whole number or counting number |
Float number |
The number in decimal |
Syntax |
Grammar or Structure of language |
Modulo |
Find the remainder |
Boolean |
True or False |
Countdown Code
user_number = input("Please enter a number: ")
number = int(user_number)
countdown_string = ""
while number > 0:
countdown_string = countdown_string + " " + str(number)
number = number-1
print (countdown_string)
|
Spelling a string out in reverse code
word = input("Type in a word: ")
reverse = ""
for letter in word:
reverse in word:
print ("Reverse: ", reverse)
|
print all elements in mylist using loop
#for loop solution
mylist = [1,2,3,4,5]
for num in mylist:
print(num)
#while loop solution
mylist = [1,2,3,4,5]
num = 0
while num < len(mylist):
print(mylist[num])
num = num + 1
|
Maxvalue
def max2(num1, num2):
maxvalue = num1
if num2 > maxvalue:
maxvalue = num2
return maxvalue
print(max2(8,99))
print(max2(5,6))
def max3(num1, num2, num3):
maxvalue = num1
if num2 > maxvalue:
maxvalue = num2
if num3 > maxvalue:
maxvalue = num3
return maxvalue
print(max3(1,2,3))
print(max3(4,5,6))
def maxlist(list):
maxvalue = mylist[0]
for item in list:
if item > maxvalue:
maxvalue = item
return maxvalue
mylist = [1,5,9,10,13]
print(maxlist(mylist))
|
Reverse
reverse = ""
letter_num = 0
word = input('type in a word: ')
while letter_num < len(word):
reverse = word[letter_num] + reverse
letter_num = letter_num + 1
if reverse == word:
print ('it is palindrome')
else:
print ('it is not palindrome')
|
def areaOfCircle
def areaOfCircle(r):
if r <= 0:
return "Error: invalid radius"
pi = 3.1415
area = pi r *2
return area
user_radius = float(input("Enter the radius: "))
print('The area of the circle is', areaOfCircle(user_radius))
|
|
|
Math
= = |
equal to |
! = |
no equal to |
< |
less than |
> |
more than |
<= |
less than or equal to |
>= |
more than or equal to |
% |
Modulo, Find the remainder |
** |
exponent |
+ |
add |
- |
subtract |
* |
multiple |
/ |
divide and quotient is float |
// |
divide and quotient is integer |
Conditionals
if... |
If the statement is true then do |
then... |
Command under then else do |
else... |
Command under else |
while... |
While that is true loop the command under the conditional loops forever |
While True |
loops forever |
for each item in name of list |
For every item in the list repeat the command under the loop that many times (a string is list too) |
Naming Conventions
Rules for naming variable: |
-letter |
-numbers |
-underscores (_) |
-can start with letters or underscores only |
-no spaces |
Valid names: |
- _mystr |
- my3 |
- Hello_there |
Invalid names: |
- 3my = "hi" --cannot start with number |
- first name = "hi" --no spaces allowed |
- first-name -- dashes are not accepted |
Function
def printDefinitions(word):
if word == "variable":
print ("""A variable is something in the memory that we can change""")
elif word == "function":
print ("""A function is to define the box of code""")
elif word == "parameter":
print ("""A parameter is value you give to the function""")
elif word == "argumant":
print ("""A argument is set of something that give to the function""")
elif word == "function":
print ("""A function call is when you call the function and it will run""")
elif word == "string":
print ("""A string is something you want to put""")
else:
print ("unknown word")
return
while True:
user_input = input("Enter words: ")
printDefinitions(user_input)
|
Receive input from the user a float and print half
user_input = input("Enter a number: ")
user_input = float(user_input)
print(user_input / 2)
|
Multification function
def multiplicationTable():
user_input = input("Enter a number: ")
num = int(user_input)
count = 1
while count <= 10:
print(num, "", count, "=", numcount)
count = count + 1
|
printFibonacci between0-50using loop
0,1,1,2,3,5,8,13,...(เค้าให้เลขมา)
num1 = 0
num2 = 1
fibonacci = num1 + num2
myoutput = "0,1"
while fibonacci: < 50:
myoutput = myoutput + "," + str(fibonacci)
num1 = num2
num2 = fibonacci:
fibonacci: = num1 + num2
print(myoutput)
|
|
|
Functions
print() |
displays information on the screen |
input() |
receives info from the user |
int() |
converts the value into an integer |
str() |
converts the value to a string |
float() |
converts the value to a floating point |
len() |
the length of the string |
# |
one line comment not include in code |
... |
Multi-line comment |
Multiplication and Exponents
string*string |
crash |
string*number |
combines the strings multiple time |
number*number |
math (multiply) |
string**number |
crash |
number**number |
exponent (math) |
string**number |
crash |
Printing True or False value using boolean
print (True)
print (2<3)
print (2! = 2)
|
Random
import random
intlist = [10,20,30,40,50]
random_int = random.choice(intlist)
print = (random_int)
fplist = [1.1,2.2,3.3,4.4,5.5]
random_fp = random.choice(fplist)
print = (random_fp)
strlist = ['earn','pim','milly']
random_str = random.choice(strlist)
print = (random_str)
mylist = ['earn',10,1.1]
random_mylist = random.choice(mylist)
print = (random_mylist)
myvar1 = 1
myvar2 = 2
myvar3 = 3
random_var = [myvar1, myvar2, myvar3]
print = (random_var)
|
Triangle And Prism
def areaOfTriangle(b, h):
area = 1/2 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 = areaOfTriangle(b, h) * l
return volume
user_lenght = float(input('Enter the lenght of the prism: '))
print ('The volume of the prism is', volumeOfPrism(user_base, user_height, user_lenght))
|
palindrome
reverse = ""
letter_num = 0
word = input('type in a word: ')
while letter_num < len(word):
reverse = word[letter_num] + reverse
letter_num = letter_num + 1
if reverse == word:
print ("it is palindrome")
else:
print ("it is not palindrome")
|
print even numbers from 1 to 100 using while loop
num = 0
while num < 100:
num = num + 2
print (num)
|
print all items in mylist using loop
mylist = ['cokezero', 'bacon', 'pepsi']
for item in mylist:
print(item)
|
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment