Show Menu
Cheatography

Vocabulary

Variable
Holds a value and can be changed
String
A list of characters such as numbers, letters, symbols
Integer number
Whole number­/Co­unting number (No decimal)
Float number
The number with decimal
Modulo
Find the remainder
Boolean
True/False
Syntax
Gramma­r/S­tru­cture of language
Function call
A call to use function
Argume­nt/­Par­ameter
Something you give to function
Function
Something that you can reuse the code

function 1

def aisha(text):
    print("" + str(text) + "")
    return

aisha(1)
aisha("hello")
aisha(1+2)
aisha("text")

def chung(text,decoration):
    print(decoration + str(text) + decoration)
    return

chung(123123213212, "++++")
chung("hello", "<<>>")

function area of circle

def areaOfCircle(r):
    if r<= 0:
        return "Error:radius <= 0"
    pi = 3.1415
    area = pi  r  r #or pi  r *2
    return area

user_radius = input("Enter the radius: ")
radius = float(user_radius)
print("The area of the circle is", areaOfCircle(radius))

Grade A Exam

theList = ["A","B","C","D"]
for item in theList:
    print(item)

whileList = ["E","F","G","H"]
s = 0
while s < len(whileList):
    print(whileList[s])
    s += 1
while True:
    user_input = input ("Enter the word: ")
    if user_input == "exit":
        break
    else:
        print(len(user_input))

def theFunction():
    while True:
        user_input = input ("Enter the word: ")
        if user_input == "stop":
            break
    return

theFunction()
        
def computeThis(a1,b2):
    return (a1*b2)
print (computeThis(4,8))

def finalFunction(string):
    print("*"+string+"*")
    return
finalFunction("Aisha")
 

Code

print (name.u­pp­er())
Make every letter capital case
print (name.l­ow­er())
Make every letter lower case
print (name.c­ap­ita­lize())
Make the first letter capital case
print (name.t­it­le())
Make the first letter and the letter after the space capital case

RANDOM

import random

# Create a list of integers
intlist =[1,2,3,4]
random_int = random.choice(intlist)
print(intlist,random_int)

# Create a list of floating point numbers
fplist = [1.0,1.2,1.4,0.5]
random_fp = random.choice(fplist)
print(fplist,random_fp)

# Create a list of strings
srtlist = ["Aisha","Eve","KamitoP","Sanya"]
random_srt = random.choice(srtlist)
print(srtlist,random_srt)

# Create a list of integer, floating point number and string
mylist = [1,2.5,"Aisha"]
random_item = random.choice(mylist)
print(mylist,random_item)

# Create a list of following variables
myvar1 = 1
myvar2 = 2 
myvar3 = 3 
varlist = [myvar1,myvar2,myvar3]
random_var = random.choice(varlist)
print(varlist,random_var)

Max Value Function

def max2(num1,num2):
    MaX = num2
    if num1 > num2:
        MaX = num1
    return MaX
def max3(num1,num2,num3):
    Max = num1
    if num2 > num1:
        if num2 > num3:
            Max = num2
    if num3 > num1:
        if num3 > num1:
            Max = num3
    return Max

first = input('Enter the first number: ')
second = input('Enter the second number: ')
third = input('Enter the thrid number: ')

print("Max value of 2 value is",max2(first,second))
print("Max value of 2 value is",max3(first,second,third))
 

Reverse Word

while True:
    word = input("Please enter the word:" )
    index = 0
    reverse = ''

    while int(index)<len(word):
        reverse = word[index] + (reverse)
        index = int(index) + 1

    print("Reverse: ", reverse)

Convert to binary

user_number = ' '

while user_number != ' 0 ' :
    user_number = input ("Enter a number to convert to binary: ")
    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)

Palindrome 9/10

def isPalindrome(word):
    reverse = ""

    for item in word:
        reverse = item + reverse

    if reverse == word:
        return True
    else:
        return False
    
while True:
    user_word = input('What is the word: ')
    length = len(user_word)
    if user_word == 'quit':
        break
    else:
        print("Length of the word is: ",(length))
        
        function_return = isPalindrome(user_word)
        if function_return == True:          
            print(user_word, "is a palindrome")
        else:
            print(user_word, "is not a palidrome")

Area of Triangle Function

def areaOfTriangle(base,height):
    return (1/2baseheight)

user_base = float(input('Enter the base of triangle: '))
user_height = float(input('Enter the height of triangle: '))

print ('The area of Triangle is: ',areaOfTriangle(user_base,user_height))

def volumeOfPrism(area,dept):
    return (area*dept)
user_dept = float(input('Enter the dept of the Prism: '))
print ('The volume of Prism is: ',volumeOfPrism(areaOfTriangle(user_base,user_height),(user_dept)))
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

            Python 3 Cheat Sheet by Finxter

          More Cheat Sheets by infinitepos