Python Cheat Sheet
/
Functions Vocabulary (cont) Area of a triangle (cont)
print() displays information on value the number or string can be store in height = float(input('Enter the
the screen valuable height of the triangle: '))
input() receives information syntax make impossible to the phase print ('The area of the triangle
from the user error is', areaoftriangle(base, height))
int() converts a value to an
integer Code
Python Palindrome
float() change number to be mystring = "hello"
while True:
decimal number print (mystring) def ispalindrome(word):
str() a list of characters firstname = input ("What is your reverse= ""
len() The length of the string first name? ") myresult= ""
" " " .... " " " comment (many lines) lastname = input("What is your last for letter in word:
name? ")
# comment(one line) reverse= letter+
fullname = firstname + " " +
import random + pick random item in the reverse
lastname
random.choice() list if word == reverse:
print (fullname) r
eturn True
Vocabulary letternumber = int(input("What is
e
lse:
letter number? ")) r
eturn False
variable holds a value and can be changed
if letternumber > len(fullname):
reverse= ""
string a list of characters such as number,
print ("invalid letter word= input("P
lease enter a
letter, symbols
number, try again! ")
word: ")
input receives information from the user
else: if word == "quit":
float number with a decimal l
etter = b
reak
number
(fullname[letternumber] )
myresult= ispalindrome(word)
syntax structure of language or grammar p
rint (letter) print("This word has",
integer whole number or counting number numberletter =
len(word),"letters")
print displays information on the screen int(input("How many times to print if myresult== True:
letter ? ")) print(True,',',word+str("
if numberletter > 100: is a palindrome"))
p
rint ("too many
e
lse:
letter too print! ") print(False,',',word+str("
e
lse:
is not a palindrome"))
p
rint (letter *
# Print: Please enter a word:
numberletter)
kayak
This word has 5 letters
Area of a triangle
True , kayak is a palindrome
def areaoftriangle(base, height): Please enter a word: mint
return base height 1/2 This word has 4 letters
base = float(input('Enter the base False , mint is not a palindrome
of the triangle: '))
By mint.sirikorn Published 10th February, 2016. Sponsored by Readability-Score.com
Last updated 21st March, 2016. Measure your website readability!
Page 1 of 4. https://readability-score.com
/
Functions Vocabulary (cont) Area of a triangle (cont)
print() displays information on value the number or string can be store in height = float(input('Enter the
the screen valuable height of the triangle: '))
input() receives information syntax make impossible to the phase print ('The area of the triangle
from the user error is', areaoftriangle(base, height))
int() converts a value to an
integer Code
Python Palindrome
float() change number to be mystring = "hello"
while True:
decimal number print (mystring) def ispalindrome(word):
str() a list of characters firstname = input ("What is your reverse= ""
len() The length of the string first name? ") myresult= ""
" " " .... " " " comment (many lines) lastname = input("What is your last for letter in word:
name? ")
# comment(one line) reverse= letter+
fullname = firstname + " " +
import random + pick random item in the reverse
lastname
random.choice() list if word == reverse:
print (fullname) r
eturn True
Vocabulary letternumber = int(input("What is
e
lse:
letter number? ")) r
eturn False
variable holds a value and can be changed
if letternumber > len(fullname):
reverse= ""
string a list of characters such as number,
print ("invalid letter word= input("P
lease enter a
letter, symbols
number, try again! ")
word: ")
input receives information from the user
else: if word == "quit":
float number with a decimal l
etter = b
reak
number
(fullname[letternumber] )
myresult= ispalindrome(word)
syntax structure of language or grammar p
rint (letter) print("This word has",
integer whole number or counting number numberletter =
len(word),"letters")
print displays information on the screen int(input("How many times to print if myresult== True:
letter ? ")) print(True,',',word+str("
if numberletter > 100: is a palindrome"))
p
rint ("too many
e
lse:
letter too print! ") print(False,',',word+str("
e
lse:
is not a palindrome"))
p
rint (letter *
# Print: Please enter a word:
numberletter)
kayak
This word has 5 letters
Area of a triangle
True , kayak is a palindrome
def areaoftriangle(base, height): Please enter a word: mint
return base height 1/2 This word has 4 letters
base = float(input('Enter the base False , mint is not a palindrome
of the triangle: '))
By mint.sirikorn Published 10th February, 2016. Sponsored by Readability-Score.com
Last updated 21st March, 2016. Measure your website readability!
Page 1 of 4. https://readability-score.com