Python Cheat Sheet: by Via

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Python Cheat Sheet

by bakumanz via cheatography.com/25889/cs/7009/

Function Sort word per line 0,01,0​​12​,​0​12​​3,01234 (cont)

print() Show inform​​ation that you want on the mystr = "​​He​l​l​o" mystring = mystring + str(num)
screen letter_num = 0 print (mystring)
while letter_num < len(my​​str):
int() Change number to be number integer
print (mystr​​[l​e​t​te​​r_num]) Definition
float() Change number to be decimal number
letter_num = letter_num + 1
input() Gain inform​​ation from user def printD​efi​nit​ion​(word):
H if word == "​var​iab​le":
str() A list of number, letter and symbols e print ("""
len() The length of the string l A variable is the the thing that can be changed.
l "​"​")
# Comment, no effect
o elif word == "​par​ame​ter​":
print ("""
Vocabulary
Selecting Largest Value A parameter is the limiting factor
Variable Hold a value and can be change "​"​")
def max2 (num1,​num2):
String A list of character such as number, elif word == "​arg​ume​nt":
if num1>num2:
letter and symbols print ("""
return num1
An argument is the identifier that you give to
Integer Whole number​​/c​o​u​nting number if num1<num2:
function
number return num2
"​"​")
Float The number in decimal def max3 (num1,​num​2,n​um3):
elif word == "​str​ing​":
number if num1>num2 and num1>num3:
print ("""
return num1
Syntax Gramma​​r/​S​t​ru​​cture of lauguage A string is something that can be repeated by
if num2>num1 and num2>num3:
the number.
Modulo Find the remainder return num2
"​"​")
Boolean True/False if num3>num1 and num3>num2:
elif word == "​fun​ction call":
return num3
print ("""
num1=i​npu​t("Enter your num1:")
Example A function call is the word you use to reuse the
num2=i​npu​t("Enter your num2:")
function.
Print (2) – integer num3=i​npu​t("Enter your num3:")
"​"​")
Print (2.5) – floating point print(​"the largest number of max3
else:
Print (“Hello”) – string is:"​,ma​x3(​num​1,n​um2​,num3))
print ("un​known word")
Print (mystr) – variable print(​"the largest number of max2
while True:
Print (mystr​​,”​H​i​”,​​2,1.0) -- commas is:"​,ma​x2(​num​1,n​um2))
user_input = input(​"​Please type the word :")
mystr = “Hi”
printD​efi​nit​ion​(us​er_​input)
mystr ← name ==
“Hi” ← value can change
print (int(1.5)) → 1 myboolean = 2 == 3 Math

print (int(“2”)) → 2 if myboolean:


== equal to
print (float(1)) → 1.0 anything to a float print ("tr​uth​")
else: != no equal to
Modulo​​/R​e​m​ainder %
print (4%2) → 0 print ("li​es") < less than
print (30%7) → 2 > more than
0,01,0​​12​,​0​12​​3,01234
<= less than or equal to
mystring = ''"
>= more than or equal to
count = 0
% Modulo, Find the remainder
while count <= 4:
mystring = mystring + str(count)
print (mystring)
count = count + 1
mystring = "​"
for num in range(5):

By bakumanz Published 5th February, 2016. Sponsored by CrosswordCheats.com


cheatography.com/bakumanz/ Last updated 23rd March, 2016. Learn to solve cryptic crosswords!
Page 1 of 3. http://crosswordcheats.com
Python Cheat Sheet
by bakumanz via cheatography.com/25889/cs/7009/

Addition Hex Boolean

string + string Combine together user_n​umber = input(​"​Enter number to convert False or True True

string + number CRASH! to hex : ") False and True False


number = int(us​er_​number)
number + number Addition (Math) True and False False
hex_string = ''
while (number > 0): True and True True
Multip​​l​i​c​ation and Exponents remainder = number % 16 False or False False
if remainder == 10:
string * number Combine that string
remainder = 'A'
string* string CRASH! Reverse Word
elif remainder == 11:
number * number Multiply (Math) remainder = 'B' while True:

string ** string CRASH! elif remainder == 12: word = input(​​"​P​lease enter a word")
remainder = 'C' index = 0
number ** number Exponent (Math)
elif remainder == 13: reverse = ' '
string ** number CRASH! remainder = 'D' while int(index) < len(word):
elif remainder == 14: reverse = word[i​​ndex] + (reverse)
Naming Convention remainder = 'E' index = int(index) + 1
elif remainder == 15: print ("Re​​verse: ", reverse)
Rule for giving name
remainder = 'F'
- letter
hex_string = str(re​mai​nder) + str(he​x_s​tring) Convert to binary
- numbers
number = number // 16
- underscore _ user_n​​umber = ' '
print ("Hex string is 0x",​hex​_st​ring)
Valid name while user_n​​umber != ' 0 ' :
- _myStr user_n​​umber = input ("Enter a number to
1*1=1
- my3 convert to binary​​")
- Hello_​​there def multip​lic​ati​onT​abl​e(num): number = int(us​​er​_​n​umber)
Invalid name multi = 0 binary​​_s​tring = ' '
- 3my=”hi” -- cannot start with number while multi < 10: while (number > 0):
- first name=”hi” multi = multi + 1 remainder = number%2
- first-name user_o​utput = num*multi binary​​_s​tring = str(re​​ma​i​n​der)+ binary​​_s​tring
- first+name print ( num,"*",​mul​ti,​"​=",u​ser​_ou​tput) number = number//2
user_num = int(in​put​("Enter the number: ")) print ("Binary string is", binary​​_s​t​ring)
Area of Circle multip​lic​ati​onT​abl​e(u​ser​_num)
Countdown Machine
"​​"​"
Fibonacci
Python Intro Assignment #2 user_n​​umber = input(​​"What number do you
name num1 = 0 want to count down? ")
student number num2 = 1 number = int(us​​er​_​n​umber)
"​​"​" fibonacci = num1 + num2 countd​​ow​n​_​string = ' '
#Ask the user for a radius of a circle output = "​0,1​" while number > 0:
user_r​​adius = input(​​"What is a radius of a while fibonacci < 50: countd​​ow​n​_​number = countd​​ow​n​_​string +
circle​​?") output = output +","+ str(fi​bon​acci) str(nu​​mber) + " "
#Convert the given radius to a floating point num1 = num2 number = number - 1
radius = float(​​us​e​r​_r​​adius) num2 = fibonacci #print​​(n​u​mber)
#Make a variable called pi fibonacci = num1 + num2 print (count​​do​w​n​_s​​tring)
pi = float(​​3.1​415) print (output)
#Calculate the area of the circle using
exponents
area = pi(ra​​diu​​s*2)
#Display the area of the circle to the user
print ("The area of the circle is", area)

By bakumanz Published 5th February, 2016. Sponsored by CrosswordCheats.com


cheatography.com/bakumanz/ Last updated 23rd March, 2016. Learn to solve cryptic crosswords!
Page 2 of 3. http://crosswordcheats.com
Python Cheat Sheet
by bakumanz via cheatography.com/25889/cs/7009/

Sort fruit list Even,Odd number

fruits = [] #an empty list even = 0


for number in range(5): odd = 0
user_fruit = input(​​"​P​lease enter a fruit") while True:
fruits.ap​​pe​n​d​(u​​ser​​_f​ruit) user_num = int(in​put​("Enter the number :"))
print ("Size of fruit list is", len(fr​​uits)) if user_num >= 0:
fruits.sort() if user_num % 2 == 0:
for fruit in fruits: even = even + 1
print ("Fruit: ", fruit) else:
odd = odd + 1
Print Name else:
print ("Even number :", even)
name = "tim GIRARD​​"
print ("Odd number :", odd)
print (name.u​​p​p​er()) → TIM GIRARD
break
print (name.l​​o​w​er()) → tim girard
print (name.c​​a​p​i​ta​​lize()) → Tim girard
For loop word
print (name.t​​i​t​le()) → Tim Girard
For word in mylist:
Guess print (word)

import random
chance = 3
score = 0
mylist = ['Hack', 'ToeyD.', 'Patte​r',​'Ti​m',​'Lily']
random​_item = random.ch​oic​e(m​ylist)
while chance > 0:
print (mylist)
print ("Ch​ances Remaining =",c​hance)
guess = input(​"​Guess a word from the above :")
if guess == random​_item:
score = score + 100
print ("That's correc​t!",​"The score is :",s​core)
random​_item = random.ch​oic​e(m​ylist)
else:
print ("Sorry, wrong choice​!")
chance = chance - 1
if guess in mylist:
print ("")
else:
print ("So​rry​,that is not even in the list!")
if chance == 0:
print ("Game Over! The word
was"​,ra​ndo​m_item)
print ("Final score: "​,score)

By bakumanz Published 5th February, 2016. Sponsored by CrosswordCheats.com


cheatography.com/bakumanz/ Last updated 23rd March, 2016. Learn to solve cryptic crosswords!
Page 3 of 3. http://crosswordcheats.com

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy