|
string + string
|
combine together
|
|
string + number
|
crash!
|
|
number + number
|
addition (math)
|
|
string * number
|
combine that string
|
|
string * string
|
Crash!
|
|
number * number
|
Multiply (Math)
|
|
string ** string
|
Crash!
|
|
number ** number
|
Exponent (Math)
|
|
string ** number
|
Crash!
|
|
if.....
|
if the statement is true then do
|
|
:then....
|
command under then else do
|
|
else....
|
command under else
|
|
while.....
|
while this is true loop the command under the conditional
|
|
While True
|
loops forever
|
|
for each
|
For every item in the list repeat
|
|
item in name of list
|
the command under the loop that many times. (a string is a list too)
|
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 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 = num2
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 mylist:
if item > maxvalue:
maxvalue = item
return maxvalue
mylist = [1,5,9,10,13]
print(maxlist(mylist))
|
variable
|
something that can change
|
|
string
|
a list of characters
|
|
integer/number
|
pos/neg natural numbers and zero
|
|
floating point
|
decimal number
|
|
lenght
|
the lenght of the string
|
|
Modulo
|
Finds the remainder
|
|
Boolean
|
True/False
|
|
Syntax
|
Grammar/Structure of the language
|
import random
intlist = [1,2,3,4,5]
random_int = random.choice(intlist)
print (random_int)
fplist = [5.0,5.1,5.2,5.3,5.4,5.5]
random_fp = random.choice(fplist)
print(random_fp)
strlist = ['dog','cat','bird','fish','fox']
random_str = random.choice(strlist)
print(random_str)
mylist = ['milly','earn','pim',1.1, 1.2]
random_item = random.choice(mylist)
print(random_item)
myvar1 = 1
myvar2 = 2
myvar3 = 3
varlist = myvar1, myvar2, myvar3
random_var = random.choice(varlist)
print (varlist, random_var)
num =int(input("Enter the number:"))
print(num * 5)
num = int(input("Enter your number:"))
remainder = num % 3
if remainder == 0:
print(num, "is divisible by 3")
else:
print(num, "is not divisible by 3")
num = int(input("Enter a number"))
if num > 0:
print(num, "is positive")
elif num < 0:
print(num, "is negative")
else:
print(num, "is zero")
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)
|
==
|
equal to
|
|
!=
|
not equal to
|
|
<
|
less than
|
|
<=
|
less than or equal to
|
|
>
|
greater than
|
|
>=
|
greater than or equal
|
|
+
|
add
|
|
-
|
substract
|
|
*
|
multiply
|
|
/
|
divide and quotient is float
|
|
//
|
divide and quotient is integer
|
|
**
|
exponent
|
|
%
|
modulo: the remainder
|
|
print0
|
displays information on the screen
|
|
input0
|
receives info from the user
|
|
int0
|
converts the value into an integer
|
|
str0
|
converts the value to a string
|
|
float0
|
converts the value to a floating point
|
|
len0
|
the lenght of the string
|
|
#
|
One line comment not include in code
|
|
'''
|
Multi-line comment
|
def areaOfTriangle(b, h):
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', areaOfTrinagle(user_base, user_height))
def volumeOfPrism(b, h, l):
volume = areaOfTriangle (b, h) * l
return volume
user_lenght = float(input('Enter the length of the prism: '))
print('The volume of the prism is', volumeOfPrism(user_base, user_height, user_lengtth))
0,01,012,0123,01234(เค้าจะให้เลขที่ต้องใส่มา)
mystring = ""
fpr num in range(5):
print (mystring)
mystring = ""
count = 0
while count < 5
mystring = mystring + str(count)
print (mystring)
count = count+1
user_number = int(input("Enter a number"))
print(user_number*5)
user_number = -100
while user_number< -1:
print(user_number)
user_number = user_number +2
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.