Show Menu
Cheatography

python.lukkaew Cheat Sheet by

Vocabulary

Variable
Something that can be changed
Module
File containing Python defini­tions and statements
String
A list of characters in order
Print
Show the inform­ation on the screen
Input
The inform­ation from the user
Syntax
Spelling and grammar of language
Integer number
Whole number or counting number
Syntax error
An error in a program that makes it impossible to parse
Len
Return the length of an object
Integer number
Whole number or counting number
Float number
The number in decimal

Print name

name = "kaewkawee PLEUMCHAROEN"

print (name.upper()) -----------> KAEWKAWEE PLEUMCHAROEN
print (name.lower()) -----------> kaewkawee pleumcharoen
print (name.capitalize()) -----------> Kaewkawee pleumcharoen
print (name.title()) -----------> Kaewkawee Pleumcharoen

Example code

mylists3 =[1, 'hello' , 2.5]
print (mylists)
print (mylists2)
print (mylists3)
#how to make a list with all numbers from 0-99
mynumbers = range(5)
for num in numbers:
print(num)

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)
 

Math

!=
Not equal to
==
Equal to
<
Less than
>
More than
<=
Less than or equal to
>=
More than or equal to
%
Modulo, Find the remainder

Fucntions

print()
show inform­ation on the screen
float()
Change number to be decimal number
str()
converts
len()
the length of the string
#
comment
input()
receives inform­ation from the user
import random + random.ch­­oi­c­e­(list)
pick random item from the list
int()
change number to be number integer

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
 

Multip­lic­ation

string­­*s­tring
crash
string­­*n­umber
combines he string multiple times
number­­*n­umber
math-m­­ul­t­iply
string­­**­n­umber
crash

Addition

string­­+s­tring
combines the string together
string­­+n­umber
crash
number­­+n­umber
Math - a­ddi­tion

Convert to Binary

user_number = ' '

while user_number != '0' ;
    user_name = 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)

Circle area code

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)
 

Word guessing game

import random

#Create a list

guesslist = ['grape', 'orange', 'chlor­opl­ast', 'ribos­ome', 'lipst­ick']

chance = 3
score = 0

print (guess­list)

while chance != 0:
random­_item = random.ch­oic­e(g­ues­slist)
user_input = input(­"­Please guess a word: ")
if user_input == random­_item:
print ("That's correc­t!")
score = score + 100
print ("Sc­ore­:", score)
else:
if user_input not in guesslist:
print ("Sorry, that isn't even in the list!")
chance = chance - 1
print ("Chance Remain­ing­:", chance)
else:
print ("Sorry, wrong choice­!")
chance = chance - 1
print ("Chance Remain­ing­:", chance)

if chance == 0:
print ("The word was", random­_item)
print ("The score is", score)
 

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.