Sunteți pe pagina 1din 4

Python Cheat Sheet

by Gameniti via cheatography.com/25846/cs/6959/

Vocabulary Multip​​l​i​c​ation and Exponents Simple Function

Variable Holds a value and can be change string * number string​​st​i​n​g...(​​number) def printdefinitions(word):

String a list of characters such as string* string Fail!! ​ ​ ​ if word == ("va​ria​ble​"):


numbers, letters, symbols ​ ​ ​ ​ ​ ​ ​ ​print ("""A variable is the
number * number Multiply
Integer Whole number​/co​unting number value that can change​"​"​")
string ** string Fail!!
number ​ ​ ​ elif word == ("fu​nct​ion​"):
number ** number Exponent
Float Number in decimal ​ ​ ​ ​ ​ ​ ​ ​print ("""A function is the
string ** number Fail!!
number blog of code that can be

Syntax Gramma​r/S​tru​cture of language reused​"​"​")


Convert to Binary String
​ ​ ​ elif word == ("pa​ram​ete​r"):
Boolean True/False
​ ​ ​ ​ ​ ​ ​ ​print ("""A parameter is
length the length of the string
user_n​umber = ' ' something given to the

Function functi​on""")
while user_name != ' '
​ ​ ​ elif word == ("ag​rum​ent​"):
Print() Show inform​ation that you want on user_n​umber = input(​"​Enter a number to
​ ​ ​ ​ ​ ​ ​ ​print ("""An agrument is
screen convert to binary​")
number = int(us​er_​number) something given to the
Int() Change number to be number integer
binary​_string = ' ' functi​on""")
input() receives info from the user ​ ​ ​ elif word == ("st​rin​g"):
str() converts the value to a string while (number > 0) ​ ​ ​ ​ ​ ​ ​ ​print ("""A string is a
remainder = number%2
float() converts the value to a floating point lsit of charac​ter​s""")
binary​_string = str(re​mai​nde​r)+​bin​ary​_string
len() The length of the string ​ ​ ​ elif word == ("fu​nction call"):
number = number//2
​ ​ ​ ​ ​ ​ ​ ​print ("""A function call
# comment,no effect
print ("Binary string is", binary​_st​ring) makes your function run"​"​")
''' Multi-line comment
​ ​ ​ ​else:
​ ​ ​ ​ ​ ​ ​ ​print ("Un​known word")
​ ​ ​ ​return
while True: #keep the loop go
forever
​ ​ ​ ​use​r_input = input(​"​Enter word:
")
​ ​ ​
​ ​ ​ ​pri​ntd​efi​nit​ion​s(u​ser​_input)

By Gameniti Published 11th February, 2016. Sponsored by CrosswordCheats.com


cheatography.com/gameniti/ Last updated 18th March, 2016. Learn to solve cryptic crosswords!
Page 1 of 4. http://crosswordcheats.com
Python Cheat Sheet
by Gameniti via cheatography.com/25846/cs/6959/

Simple Function Simple Function (cont) Addition

def printdefinitions(word): Unknown word string + string combine together


​ ​ ​ if word == ("va​ria​ble​"): Enter word: function string + number Fail
​ ​ ​ ​ ​ ​ ​ ​print ("""A variable is the A function is the blog of code that
number + number plus
value that can change​"​"​") can be reused
number - number minus
​ ​ ​ elif word == ("fu​nct​ion​"): Enter word: variable
​ ​ ​ ​ ​ ​ ​ ​print ("""A function is the A variable is the value that can
Sample code
blog of code that can be change
mystr = "hellp THERE"
reused​"​"​") Enter word:
print (mystr.up​per()) -all letters
​ ​ ​ elif word == ("pa​ram​ete​r"): area/v​​olume of
will become big HELP THREE
​ ​ ​ ​ ​ ​ ​ ​print ("""A parameter is
print (mystr.lo​wer()) -all letters
something given to the Symbols
will become small help three
functi​on""")
== equal to print (mystr.ca​pit​ali​ze()) -First
​ ​ ​ elif word == ("ag​rum​ent​"):
!= not equal to letter of first word will become
​ ​ ​ ​ ​ ​ ​ ​print ("""An agrument is
< less than big Help three
something given to the
functi​on""") <= less than or equal to print (mystr.ti​tle())- first

> greater than letter of each words will become


​ ​ ​ elif word == ("st​rin​g"):
big Help Three
​ ​ ​ ​ ​ ​ ​ ​print ("""A string is a >= greater than or equal to
lsit of charac​ter​s""") + add
Example
​ ​ ​ elif word == ("fu​nction call"):
- subtract
​ ​ ​ ​ ​ ​ ​ ​print ("""A function call
* multiply
makes your function run"​"​") Print (2) – integer
/ divide and quotient is float
​ ​ ​ ​else: Print (2.5) – floating point
// divide and quotient is integer Print (“Hello”) – string
​ ​ ​ ​ ​ ​ ​ ​print ("Un​known word")
** exponent Print (mystr) – variable
​ ​ ​ ​return
Print (mystr​​,​”​H​i​”,​​2,1.0) -- commas
while True: #keep the loop go % modulo: the remainder

forever mystr = “Hi”


​ ​ ​ ​use​r_input = input(​"​Enter word: mystr ← name
") “Hi” ← value can change

​ ​ ​
print (int(1.5)) → 1
​ ​ ​ ​pri​ntd​efi​nit​ion​s(u​ser​_input)
print (int(“2”)) → 2
Enter word: hello
print (float(1)) → 1.0 anything to a float

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

By Gameniti Published 11th February, 2016. Sponsored by CrosswordCheats.com


cheatography.com/gameniti/ Last updated 18th March, 2016. Learn to solve cryptic crosswords!
Page 2 of 4. http://crosswordcheats.com
Python Cheat Sheet
by Gameniti via cheatography.com/25846/cs/6959/

Area of the circle Maxlist Capital letter

def maxlist(list):
def areaOf​Cir​cle(r): ​ ​ ​ ​max​value = list[0] name = "tim GIRARD​​"
pi = 3.1415 ​ ​ ​ for item in list:
area = pir*2 ​ ​ ​ ​ ​ ​ ​ if item > maxvalue: print (name.u​​p​p​er()) → TIM GIRARD
return area print (name.l​​o​w​er()) → tim girard
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​max​value = item
user_r​adius = float (input​("Enter the radius: ")) print (name.c​​a​p​i​ta​​li​​ze()) → Tim girard
​ ​ ​ ​return maxvalue
print ('The area of the circle print (name.t​​i​t​le()) → Tim Girard
is',ar​eaO​fCi​rcl​e(u​ser​_ra​dius)) mylist = [1,2,3​,4,​55,​66,​777​,0,1]
print(​max​lis​t(m​ylist)) circle area
MaxValue 777
def areaofcircle(radius):
def max2(num1,num2): ​ ​ ​ if radius <= 0:
Naming Conven​​tions
​ ​ ​ ​max​value = num1 ​ ​ ​ ​ ​ ​ ​ ​return "​Error: invalid
​ ​ ​ if num2 > maxvalue: Rule for giving name raadiu​s"
​ ​ ​ ​ ​ ​ ​ ​max​value = num2 -letter ​ ​ ​ pi = 3.1415
​ -numbers ​ ​ ​ area = pi (radius*2)
​ ​ ​ ​return maxvalue -under​score_ ​ ​ ​ ​return area
print (max2(​4,5)) Valid name user_r​adius = float(​inp​ut(​"​Enter
print (max2(​33,5)) - _myStr the radius: "))
def max3(n​um1​,nu​m2,​num3): - my3 print('The area of the circle is',
​ ​ ​ - Hello_​there areaof​cir​cle​(us​er_​rad​ius))
​ ​ ​ ​max​value = num1 Invalid name Enter the radius: 2
​ ​ ​ if num2 > maxvalue: - 3my="hi​" -- cannot start with The area of the circle is 12.566
​ ​ ​ ​ ​ ​ ​ ​max​value = num2 number Enter the radius: 0
​ ​ ​ if num3 > maxvalue: - first name = "​hi" The area of the circle is Error:
​ ​ ​ ​ ​ ​ ​ ​max​value = num3 - first-name invalid raadius
​ ​ ​ - first+name
​ ​ ​ ​return maxvalue Countdown Number
print (max3(​1,2,3))
5
user_n​umber = input(​"​Please enter the
33
number​")
3 number = int(us​er_​number)
countd​own​_string = "​"
while number​>0:
countd​own​_string = countd​own​_string +
str(nu​mber)
number = number - 1
print (count​dow​n_s​tring)

By Gameniti Published 11th February, 2016. Sponsored by CrosswordCheats.com


cheatography.com/gameniti/ Last updated 18th March, 2016. Learn to solve cryptic crosswords!
Page 3 of 4. http://crosswordcheats.com
Python Cheat Sheet
by Gameniti via cheatography.com/25846/cs/6959/

Palindrome Short word per line Basic Function (cont)

def isPalindrome(word): mystr = "Hello" ​ ​ ​ ​return number * 2


​ ​ ​ ​reverse = "​" letter_num = 0 print (doubl​eit(3))
​ ​ ​ ​let​ter​_num=0 while letter_num < len(my​str): print (doubl​eit​(do​ubl​eit​(4)))
​ ​ ​ ​while ​ ​ ​ ​ ​ ​ ​ ​ ​print (mystr​[le​tte​r_num]) hello it's bacon
letter​_nu​m<l​en(​use​r_i​nput): ​ ​ ​ ​ ​ ​ ​ ​ ​let​ter_num = letter_num + opal
​ ​ ​ ​ ​ ​ ​ ​reverse = 1 mopalm
user_i​npu​t[l​ett​er_​num​]+r​everse H 6
​ ​ ​ ​ ​ ​ ​ ​let​ter_num = letter​_num+1 e
​ ​ ​ if revers​e==​word: l
​ ​ ​ ​ ​ ​ ​ ​return True l
​ ​ ​ ​else: o
​ ​ ​ ​ ​ ​ ​ ​return False
​ ​ ​ Basic Function
while True :
def myprint(text):
​ ​ ​ ​use​r_input = input(​"​Enter a
​ ​ ​ ​print (" ​" + str(text) + "​ ​")
word")
​ ​ ​ ​return
​ ​ ​ if user_input == "​qui​t":
myprin​t("o​pal​")
​ ​ ​ ​ ​ ​ ​ ​break
hello it's bacon
​ ​ ​
opal
​ ​ ​ ​isPal =
def myprin​tne​w(text, decora​tion):
isPali​ndr​ome​(us​er_​input)
​ ​ ​ ​print (decor​ation + str(text)
​ ​ ​
+ decora​tion)
​ ​ ​ if isPal == True:
​ ​ ​ ​return
​ ​ ​ ​ ​ ​ ​ ​print (user_​inp​ut,'is
myprin​tne​w("o​pal​", "​m")
parind​orm')
hello it's bacon
​ ​ ​ ​else:
opal
​ ​ ​ ​ ​ ​ ​ ​print (user_​inp​ut,'is not
mopalm
parind​orm')
def double​it(​num​ber):
​ ​ ​ ​ ​ ​ ​ ​break
Enter a word113311
113311 is parindorm
Enter a word123
123 is not parindorm
Enter a wordquit

By Gameniti Published 11th February, 2016. Sponsored by CrosswordCheats.com


cheatography.com/gameniti/ Last updated 18th March, 2016. Learn to solve cryptic crosswords!
Page 4 of 4. http://crosswordcheats.com

S-ar putea să vă placă și