Você está na página 1de 2

Nancy's Python Cheat Sheet Cheat Sheet

by Mnasnan (mnasnan) via cheatography.com/25754/cs/6873/


Naming Convention

Symbol

Rule for giving name

==

Equal

- letter

!=

Not equal

>=

More than or equal

Valid name

<=

Less than or equal

- _myStr

% (Madulo)

Find the remainder

- numbers
- underscore _

- my3

Palindrome (cont)

Reverse Word (cont)

print (user_word,
"is not a Palindrome")

Palindrome

Invalid name
- 3my=hi -- cannot start with

Area of Triangle

+ 1

def areaOfTriangle(base,

number

index = 0

- first name=hi

reverse = ''

- first-name

while int(index) <

- first+name

len(user_word):

Convert binary to decimal


num = str(int(input("Enter
a binary to convert to
decimal ")))
dec = int(num, 2)
print ("The decimal number
is", dec)

reverse =
user_word[index] +
(reverse)
index = int(index)
+ 1
if user_word ==
reverse:
return True

Sort word per line


mystr = "Hello"
letter_num = 0

else:
return False
while True:
user_word =

while letter_num <

input("Please enter a

len(mystr):

word: ")
if user_word ==

print
(mystr[letter_num])
letter_num =
letter_num + 1

"quit":
break

print("Length of the",
user_word, "is",
len(user_word))

Palindrome =
isPalindrome(user_word)
if Palindrome == True:
print (user_word,
"is a Palindrome!")

print ("Reverse: ",


reverse)

return 0.5 base hight

def isPalindrome(word):

word[index] + (reverse)
index = int(index)

hight) :

- Hello_there

reverse =

user_base =
float(input("Enter the

Multiplication and Exponents


string * number

Combine that
string

base of the triangle: "))


user_height =

string* string

CRASH!

float(input("Enter the

number *

Multiply (Math)

hight of the triangle: "))

number

print ("The area of the

string ** string

CRASH!

triangle is",

number **

Exponent (Math)

areaOfTriangle(user_base,

number

user_height))

string **

def volumeOfPrism(area,

number

CRASH!

high) :
return area * high
user_prism_high =

Countdown Machine
number = int(input("What

float(input("Enter the
hight of the prism: "))
print ("The volume of the
prism is",

number do you want to


count down? "))
countdown_string = ' '
while number > 0:

volumeOfPrism(areaOfTriang
le(user_base,

countdown_number =
countdown_string +

user_height),

str(number) + " "

user_prism_high))

number = number - 1

Reverse Word

print (countdown_string)

while True:
word = input("Please
enter a word")
index = 0
reverse = ' '

while int(index) <
len(word):

else:

By Mnasnan (mnasnan)

Published 3rd February, 2016.

Sponsored by Readability-Score.com

cheatography.com/mnasnan/

Last updated 16th March, 2016.

Measure your website readability!

Page 1 of 2.

https://readability-score.com

Nancy's Python Cheat Sheet Cheat Sheet

by Mnasnan (mnasnan) via cheatography.com/25754/cs/6873/


Convert decimal to binary

Guess word game (cont)

Area of Circle by Function

Create List

user_number = ' '

score = 0

def areaOfCircle(r):

create a function name:

while user_number != ' 0

print (guesslist)

pi = 3.1415

createList

' :

while chance != 0:

area = pi r * 2

argument: quitword

return area

return: a list

user_number = input
("Enter a number to
convert to binary")
number =
int(user_number)
binary_string = ' '
while (number > 0):
remainder =
number%2

random_item =
random.choice(guesslist)
user_input =

the circle: ")

word: ")

radius =

if user_input ==
random_item:
print ("That's

str(remainder)+
binary_string
number = number//2
print ("Binary string
is", binary_string)

print ("Score:",
score)

for number in range(5):


user_fruit =
input("Please enter a

fruits.append(user_fru

that isn't even in the


list!")

print ("Size of fruit list


is", len(fruits))
fruits.sort()
for fruit in fruits:
print ("Fruit: ",

guesslist = ['grape',

areaOfCircle(radius))

item: ")
if user_word ==
quitword:

user_radius = input("What

return mylist
#returns the list and
exits the function
duplicateword =

radius =

False

float(user_radius)

for item in

pi = float(3.1415)

mylist:

area = pi (radius*2)

if user_word
== item:

circle is", area)

ord = True

Function (Ex.)

if (duplicateword

Remaining:", chance)

print ("Sorry,
wrong choice!")
chance =

def myprint2(text,

== True):

decoration): #Text and


decoration is a parameter
print (decoration +
str(text) + decoration)

chance - 1
print ("Chance

print ("The word was",


random_item)

import random

input("Please enter a list

duplicatew

if chance == 0:
Guess word game

user_word =

circle is",

chance - 1

Remaining:", chance)

fruit)

print("The area of the

print ("The area of the

chance =

else:

it)

forever

circle?")

if user_input not

print ("Chance

fruit")

float(user_radius)

is a radius of a

else:

print ("Sorry,

fruits = [] #an empty list

while True: #loop

Area of Circle

100

mylist = [] #empty
list

score = score +

in guesslist:
Sort fruit list

def createList(quitword):

input("Enter the radius of

input("Please guess a

correct!")

binary_string =

user_radius =

print ("The score is",

return

'orange', 'chloroplast',

+++++Hello+++++

'ribosome', 'lipstick']

-=-=-=-=Hello-=-=-=-=

chance = 3

<<<<<Hello<<<<<

mylist.append(

the list

myprint2("Hello", "-=-=-

"<<<<<")

score)

else:

user_word to the end of

"+++++")

myprint2("Hello",

("Duplicate Word!")

user_word) #Adds the

myprint2("Hello",

=-=")

print


userlist =
createList('stop')
#Function Call
print (userlist)

By Mnasnan (mnasnan)

Published 3rd February, 2016.

Sponsored by Readability-Score.com

cheatography.com/mnasnan/

Last updated 16th March, 2016.

Measure your website readability!

Page 2 of 2.

https://readability-score.com

Você também pode gostar