Show Menu
Cheatography

Commands Cheat Sheet by

Commands

print()
A command used to print something
int()
A command used to convert any kinds of number into integer
float()
A command used to convert any kinds of number into decimal numbers
input()
A command used to obtain inform­­ation from the user
str()
A command used to convert letters or numbers into a string
len()
A command used to determine the length of a string
#
A command used to make statements a comment that does not have any effect on the coding

Mathem­­atical operators

==
equal to
!=
not equal to
<
less than
>
more than
<=
less than or equal to
>=
more than or equal to
%
Modulo, used to find a remainder

Variable names condition

Following conditions must me followed:
-letters
-numbers
-under­­score

Valid name
- _myStr
- my3
- Hello_­­­there

Invalid name
- 3my=”hi” -- cannot start with number
- first name=”hi”
- first-name
- first+name

Guess a word game

import random
Chances=5
Score=0


while Chance­­s>0:

mylist­­=[­'­M­os­­cow­­',­'­B­er­­lin­­',­'­V­an­­cou­­ve­r­'­,'­­Sai­­nt­P­e­te­­rsb­­ur­g­'­,'­­Chi­­cago']
random­­_i­t­e­m=­­ran­­do­m.c­h­o­ic­­e(m­­ylist)
print(­­my­list)
print (rando­­m_­item)
user_g­­ue­s­s­=i­­npu­­t(­"­guess a word:")
if user_g­­ue­s­s­==­­ran­­do­m­_­item:
print(­­"­C­hances remain­­in­g­:­"­,C­­hances)
print(­­"­C­orrect guess")
Score=­­Sc­o­r­e+100
else:
Chance­­s=­C­h­an­­ces-1
print(­­"­S­orry, wrong choice­­")
print(­­"­C­hances remain­­in­g­:­"­,C­­hances)
print(­­"The correct answer was:",r­­an­d­o­m_­­item)

print(­­"Your score now is:"­­,S­core)
 

Vocabulary

Variable
Hold a value and can be changed. It can also be set to a string of words
String
A list of character such as number­­,l­etter and symbols
Integer number
Whole number­­/c­o­u­nting number
Floating point
Numerical values in decimal
Syntax
Gramma­­r/­S­t­ru­­cture of language
Modulo
Used to find a remainder
Boolean
True or False

Example

Print (2) – integer
Print (2.5) – floating point
Print (“Hello”) – string
Print (mystr) – variable
Print (mystr­­­,­”­H­­i­­”,­­­2,1.0) -- commas

mystr = “Hi”
mystr ← Variable
“Hi” ← value can be changed

print (int(1.5)) → 1
print (int(“2”)) → 2
print (float(1)) → 1.0

Modulo­­­/­R­e­­m­­ainder %
print (4%2) → 0
print (30%7) → 2

Binary number conversion

user_n­­­umber = ' '

while user_n­­­umber != ' 0 ' :
user_n­­­umber = input ("Enter a number to convert to binary­­­")
number = int(us­­­e­r­_­­n­­umber)
binary­­­_­s­tring = ' '

while (number > 0):
remainder = number%2
binary­­­_­s­tring = str(re­­­m­a­i­­n­­der)+ binary­­­_­s­tring
number = number//2

print ("Binary string is", binary­­­_­s­t­­ring)
 

Addition

string­­+s­tring
Combine the two strings together
string­­+n­umber
CRASH!
number­­+n­umber
Add numbers together

Multip­­li­c­ation and exponents

string­­*n­umber
Duplicate the strings x time = numerical r values
string­­*s­tring
CRASH!
number­­*n­umber
multiply numbers together
string­­**­s­tring
CRASH!
number­­**­n­umber
Expone­­nt­(­math)
string­­**­n­umber
CRASH!

Reverse Word

while True:
word = input(­­­"­­P­lease enter a word")
index = 0
reverse = ' '

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

print ("Re­­­v­erse: ", reverse)

Hexade­­cimal conversion

#write a program that converts a number to hexade­­cimal

while True:
#get a number from the user
user_n­­umber= input(­­"­C­hoose your number: ")

#convert to integer
number­­=i­n­t­(u­­ser­­_n­u­mber)

hex_st­­ri­ng=''
while(­­nu­m­b­er­­>0)­­:#the number is greater than 0
remainder= number­­%1­6#use modulo %
number= number­­//­1­6­#must use//when you divide
if remainder ==10:
remain­­de­r='A'
elif remain­­de­r­==11:
remain­­de­r='B'
elif remain­­de­r­==12:
remain­­de­r='C'
elif remain­­de­r­==13:
remain­­de­r='D'
elif remain­­de­r­==14:
remain­­de­r='E'
elif remain­­de­r­==15:
remain­­de­r='F'
hex_st­­ring= str(re­­ma­i­n­der)+ hex_string


#after the loop print the hex string
print(­­'h­e­x­ad­­ecimal string is 0x'+he­­x_­s­t­ring)

#expected output­­-5=101
#expected output­­-3=11
#expected output-2=1
 

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.