Show Menu
Cheatography

Functions

print()
displays inform­ation on the screen
input()
receives inform­ation from the user
int()
converts a value to an integer
float()
change number to be decimal number
str()
a list of characters
len()
The length of the string
" " " .... " " "
comment (many lines)
#
commen­t(one line)
import random + random.ch­oice()
pick random item in the list

Vocabulary

variable
holds a value and can be changed
string
a list of characters such as number, letter, symbols
input
receives inform­ation from the user
float number
number with a decimal
syntax
structure of language or grammar
integer
whole number or counting number
print
displays inform­ation on the screen
value
the number or string can be store in valuable
syntax error
make impossible to the phase

Code

mystring = "hello"
print (mystring)
firstname = input ("What is your first name? ")
lastname = input("What is your last name? ")
fullname = firstname + " " + lastname
print (fullname)

letternumber = int(input("What is letter number? "))
if letternumber > len(fullname):
      print ("invalid letter number, try again! ")
else:

      letter = (fullname[letternumber] )
      print (letter)

      numberletter = int(input("How many times to print letter ? "))

      if numberletter > 100:
            print ("too many letter too print! ")
      else:
            print (letter * numberletter)

Area of a triangle

def areaoftriangle(base, height):
   return base height 1/2

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

print ('The area of the triangle is', areaoftriangle(base, height))

Python Palindrome

while True:
    def ispalindrome(word):
        reverse= ""
        myresult= ""
        for letter in word:
            reverse= letter+ reverse
        if word == reverse:
            return True
        else:
            return False
            reverse= ""
    word= input("Please enter a word: ")
    if word == "quit":
        break
    myresult= ispalindrome(word)
    print("This word has", len(word),"letters")
    if myresult== True:
        print(True,',',word+str(" is a palindrome"))
    else:
        print(False,',',word+str(" is not a palindrome"))
# Print: Please enter a word: kayak
This word has 5 letters
True , kayak is a palindrome
Please enter a word: mint
This word has 4 letters
False , mint is not a palindrome

Recieve number and determine number

9 is divisible by3
7 is not divisible by 3
usernumber= input("Please enter the a number: ")
remainder= usernumber%3
if remainder ==0:
 print(usernumber, "is divisible by 3")
else:
 print(usernumber, "is not divisible by 3")
4 is positive 0 is zero -8 is negative
usernum= input("Pls enter the number: ")
if usernum>0:
  print(usernum, "is positive")
elif usernum==0:
  print(usernum, "is zero")
else:
  print(usernu, "is negative")

areaOF­Ellipse

# the function should be given two parameters and should return the area
def areofellipse(r1r2):
 area= 3.14r1r2
 return(area)
r1= float(input("Enter radius1: "))
r2= float(input("Enter radius2: "))
area= areaofellipse(r1,r2)
print(area)
 

Operation

==
equal to
!=
not equal
<
less than
>
greater than
<=
less than or equal to
>=
greater than or equal to
%
Modulo, find the remainder

Multip­lic­ation and Exponents

string * number
combine that string multiple times
string * string
crash
number * number
math - multiply
string ** string
crash
number ** number
math - exponents
string ** number
crash

Reverse word

while 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)

Random choice code

import random
 mylist = ['cat','dog','chicken','bird','fish']
 score = 0
 chances = 3
 start_over = 0
 random_item = random.choice(mylist)

 while chances > 0:
   start_over = 0
   random_item = random.choice(mylist)
     
   while start_over < 1:
     print ("-=-=-=-=-=-=-=-=-=")
     print ("Guessing Game")
     print ("-=-=-=-=-=-=-=-=-=")
     print("words:", mylist)
     guess = input("Guess a word: ")

     if (guess in mylist):
             
       if(guess == random_item ):
         print("That's correct!")
         score = score + 100
         print("Score:", score)
         start_over = 2
       else:
         print("Sorry,wrong choice!")
         chances = int(chances) -1
     else:
       print("Sorry, that is not even in the list")
       chances = int(chances) -1

     if(chances > 0):
        print("Chances remaining:",chances)
     else:
        start_over = 2
        print("Game Over! The word was ", random_item)
        print("Chance remaining:", chances)
        print("Final score:", score)

Convert number to binary

user_number = " "

while user_number != "0":
      
   user_number = input ("enter a number")
   number = int(user_number)
   binary_string = " "

while (number>0):
      
   remainder = number % 2
   binary_string = str(remainder) + binary_string
   number = numbe//2
   print (number)

   print ("binary string is ", binary_string)

code

def createlist(quitword):
   mylist= []

   while True:
       item= input("Please enter a list item: ")
       if item== quitword:
          return mylist
       duplicateword= False
       for myvar in mylist:
          if myvar== item:
            duplicateword= True

       if duplicateword= True:
            print ("Duplicate word! ")
       else:
            mylist.append(item)

mylist= createlist ("stop")
print(mylist)

For loop

0
1
12
mystring= ""
for number in range (3)
     mystring= mystring+ str(number)
     print (mystring)
or
mystring=""
count=0
while count<5:
  mystring= mystring+str(count)
  print(mystring)
  count= count+1
 

Addition

string + string
combine togrther
string + number
crash
number + number
math - addition

List code

shoppinglist = ['tshirt', 'pants', 'socks']

for myvariable in shoppinglist:
     print (myvariable)
or
mylist= [1,2,3,4]
number= 0
while number<len(mylist):
    print (mylist[number])
    number= number+1

Random code

import random

mylist = ['Dog','Fish','Cat','Bear']
counter = 0

while counter < 10:
          random_item = random.choice (mylist)
          print (random_item)
          counter = counter + 1

Print name

name = "time GIRARD"

print (name.upper()) > TIM GIRARD
print (name.lower()) > time girard
print (name.capitalize()) > Tim girard
print (name.title()) > Tim Girard

Area of circle

while True:

       user_radius = input("What is the radius? ")
       radius = float(user_radius)
       pi = 3.1415
       area = pi radius * 2
       print ("The area of the circle is", area)

Loop doesn't go forever

gameover= 0

while (gameover == 0):
   print("hello")
   gameover= 1

while loop

wlist= [2,4,5,6,7,8]
index= 0
while index< len(wlist):
    print(wlist[index])
    index= index+1

code

# receives input from the user in a loop. convert the input to an integer and print out that integer multiplied by 10

while True:
   usernumber= input("Please enter the number: ")
   answer= int(usernumber)*10
   print (answer)

Count down code

#create a program that receives a number from the user and count down from that number on the same line

#receive the number from the user as a string
user_number= input("enter number")

#convert the user number to an integer
number = int(user_number)

#setup the countdown string
countdown_string = " "

while number > 0:
       #add the number to the string
       #subtract 1 from the number
       countdown_string = countdown_string + str(number) + " "
       number = number-1

print (countdown_string)

#output should look like this
# if the user enter 5:
#5 4 3 2 1
#print (countdown_string)

Word length

while True:
    usernumber= input("Please enter a word: ")
    if usernumber == "exit":
        break
    print(len(usernumber))

# Please enter a word: hello
5
Please enter a word: pls
3
Please enter a word: exit

True False

create function= def
True or anyt­hing is True
False and anyt­hing is False

User enters 12.5, print out 6.25

number= float(input("Please enter number: "))
print (number/2)

Even number

# print all the even numbers from 1 to 100 using while loop

number=0
while number<100:
    variable= number+2
    print(variable)

Multip­lic­ation Table

usernum= int(input("Enter a number: "))
numlist= [1,2,3,4,5,6,7,8,9,10]
for num in numlist:
   answer= usernum*num
   print (user,"*",num,"=",answer)
 

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.