Show Menu
Cheatography

Python cheat sheet muids Cheat Sheet by

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­,letter and symbols
Integer number
Whole number­/co­unting number
Floating point
Numerical values in decimal
Syntax
Gramma­r/S­tru­cture of language
Modulo
Used to find a remainder
Boolean
True or False

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
 

Addition

string­+string
Combine the two strings together
string­+number
CRASH!
number­+number
Add numbers together

Multip­lic­ation and exponents

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

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

Multip­lic­ation table

number­=in­t(i­npu­t("enter a number­"))
def multip­lic­ati­onTable (number):

count=1
while count<=10:
print(­num­ber­,"",­cou­nt,­"­=",n­umb­er­count)
count=­count+1
multip­lic­ati­onT­abl­e(n­umber)

Fibonacci

num1=0
num2=1
fibona­cci­=nu­m1+num2
while fibona­cci­<50:
print(­fib­onacci)
num1=num2
num2=f­ibo­nacci
fibona­cci­=nu­m1+num2

positive integer and negative integer

evenco­unt=0
oddcount=0
while True:
num=in­t(i­npu­t("enter a positive intege­r"))
if num<0:
print(­"even number­s:",­eve­ncount)
print(­"odd number­s:",­odd­count)
break
else:
if(num­%2)==0:
evenco­unt­=ev­enc­ount+1
else:
oddcou­nt=­odd­count+1
 

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

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­­verse: ", reverse)

Binary number conversion

#write a program that converts a number to binary

while True: #forever loop is while True:
#get a number from the user
user_n­umber= input(­"­Choose you number­")

#convert to integer
number­=in­t(u­ser­_nu­mber)

binary­_st­ring=''
while(­num­ber­>0)­:#the number is greater than 0)
remainder= number­%2#use modulo %
binary­_st­ring= str(re­mai­nder)+ binary­_string
number= number­//2­#must use//when you divide

#after the loop print the binary string
print(­'binary string is',bi­nar­y_s­tring)

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

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(­"­Choose your number: ")

#convert to integer
number­=in­t(u­ser­_nu­mber)

hex_st­ring=''
while(­num­ber­>0)­:#the number is greater than 0
remainder= number­%16#use modulo %
number= number­//1­6#must use//when you divide
if remainder ==10:
remain­der='A'
elif remain­der­==11:
remain­der='B'
elif remain­der­==12:
remain­der='C'
elif remain­der­==13:
remain­der='D'
elif remain­der­==14:
remain­der='E'
elif remain­der­==15:
remain­der='F'
hex_st­ring= str(re­mai­nder)+ hex_string


#after the loop print the hex string
print(­'he­xad­ecimal string is 0x'+he­x_s­tring)

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

Guess a word game

import random
Chances=5
Score=0


while Chance­s>0:

mylist­=['­Mos­cow­','­Ber­lin­','­Van­cou­ver­','­Sai­ntP­ete­rsb­urg­','­Chi­cago']
random­_it­em=­ran­dom.ch­oic­e(m­ylist)
print(­mylist)
print (rando­m_item)
user_g­ues­s=i­npu­t("guess a word:")
if user_g­ues­s==­ran­dom­_item:
print(­"­Chances remain­ing­:",C­hances)
print(­"­Correct guess")
Score=­Sco­re+100
else:
Chance­s=C­han­ces-1
print(­"­Sorry, wrong choice­")
print(­"­Chances remain­ing­:",C­hances)
print(­"The correct answer was:",r­and­om_­item)

print(­"Your score now is:"­,Score)

selecting largest values

def max2 (num1,­num2):
if num1>num2:
return num1
if num1<num2:
return num2

def max3 (num1,­num­2,n­um3):
if num1>num2 and num1>num3:
return num1
if num2>num1 and num2>num3:
return num2
if num3>num1 and num3>num2:
return num3
num1=i­npu­t("Enter your num1:")
num2=i­npu­t("Enter your num2:")
num3=i­npu­t("Enter your num3:")

print(­"the largest number of max3 is:"­,ma­x3(­num­1,n­um2­,num3))
print(­"the largest number of max2 is:"­,ma­x2(­num­1,n­um2))

Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us!

We offset our carbon footprint via Ecologi
 

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.