Cheatography
https://cheatography.com
VocabularyVariable | Hold a value and be change | String | A list of character such as number, letter and symbols | Integer number | Whole number/counting number | Float number | The number in decimal | Syntax | Grammar/Structure of language | Modulo | Find the remainder | Boolean | True/False |
Functionprint() | Show the information that you want on the screen | int() | Change number to be number integer | float() | Change number to be decimal number | input() | Gain information from user | str() | A list of number, letter and symbols | len() | The length of the string | # | Comment, no effect |
Forever While Loopwhile True: # forever
user_input = input('Enter a number: ')
number = int(user_input)
print ('The number squared is', number ** 2)
|
Conditional While Loopcount = 0 # start at zero
while count < 10: # loop while count is less than 10
print(count) #will print numbers 0 - 9
count = count + 1 # must increase count
|
Listsmylist = [2,3,4,5] # create a list
#select an item from a list
print (mylist[0]) #selects first item and displays 2
# len() determines the length of the list
print (len(mylist)) # displays 4
mylist.append(5) # adds an item to the end of the list
|
| | Multiplication and Exponentsstring * number | Combine that string | string* string | CRASH! | number * number | Multiply (Math) | string ** string | CRASH! | number ** number | Exponent (Math) | string ** number | CRASH! |
Math== | equal to | != | no equal to | < | less than | > | more than | <= | less than or equal to | >= | more than or equal to | % | Modulo, Find the remainder |
Additionstring + string | Combine together | string + number | CRASH! | number + number | Addition (Math) |
Convert number to hexadecimalwhile True:
#get a number from the user
user_number = input("Please enter a number: ")
#convert to integer
number = int(user_number)
hex_string = ''
while (number > 0):#the number is greater than 0)
remainder = number % 16 #use Modulo %
if remainder == 10:
remainder = 'A'
elif remainder == 11:
remainder = 'B'
elif remainder == 12:
remainder = 'C'
elif remainder == 13:
remainder = 'D'
elif remainder == 14:
remainder = 'E'
elif remainder == 15:
remainder = 'F'
hex_string = str(remainder) + hex_string #remainder + hex string
number = number // 16#must use // when you divide
#after the loop print the hex string
print ("Hexadecimal string is 0x" + hex_string)
#expected output - 5 = 101
#excepted output - 3 = 11
#excepted output - 2 = 10
|
| | Reverse Wordwhile True:
word = input("Please enter a word")
index = 0
reverse = ' '
while int(index) < len(word):
reverse = word[index] + (reverse)
index = int(index) + 1
print ("Reverse: ", reverse) |
Create function# how to create a function
def nameOfFunction (myvar1, myvar2): # parameters or argu ments
print ("hello") #must indent each line that is part of the function
return myvar1 + myvar2
# function call
nameOfFunction ('hi', 'there') # a value for each argument
#write a function
# name : areaOfTriangle
# parameters : base height
# return : area
def areaOfTriangle (base, height):
area = 0.5*base*height
return area
|
Decision Making/Conditional Statementsif 3 < 2: #if statement must compare two Booleans
print ('3 is less than 2')
elif 4 < 2: #can have 0 or more elif statements
print ('4 is less than 2')
elif 5 < 2:
print ('5 is less than 2')
else: #can have 0 or 1 else statement at the end
print ('none of the above are True')
|
|
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment