Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
66 views

Pinpin Python-3

The document provides a cheat sheet for Python that includes: 1) Common math symbols and their meanings used in Python; 2) Examples of basic Python code for variables, strings, conditionals, functions, and lists; 3) Short descriptions and examples for common Python concepts like variables, conditionals, loops, and functions.

Uploaded by

api-295782748
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Pinpin Python-3

The document provides a cheat sheet for Python that includes: 1) Common math symbols and their meanings used in Python; 2) Examples of basic Python code for variables, strings, conditionals, functions, and lists; 3) Short descriptions and examples for common Python concepts like variables, conditionals, loops, and functions.

Uploaded by

api-295782748
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Python Cheat Sheet

by pinpin via cheatography.com/25830/cs/6963/


Math of Symbols

What's is your name? code

List Random (cont)

==

Equal to

mystring = "hello"

ramdom_str =

!=

No equal to

print (mystring)

random.choice(strlist)

<

Less than

firstname = input( "what is your

print(ramdom_str,strlist)

>

More than

first name?")

mylist =

<=

Less than or equal to

lastname = input( "what is your

[1,10,100,2,20,200,'Pin','Anpan','B

>=

More than or equal to

last name?")

ella']

fullname = firstname + " " +

ramdom_item =

Modulo, find the remainder

lastname

random.choice(mylist)

Add

print (fullname)

print(ramdom_item,mylist)

Subtract

letternumber = int(input( " what

myvar1 = 1

Multiplication

is letter number? " ))

myvar2 = 2

**

Exponent

if letternumber >len(fullname):

myvar3 = 3

Divide and quotient is float

//

Divide and quotient is integer

print ( " invalid letter


number, try again! " )

random_var =

else:

random.choice(varlist)

letter = (

Text

fullname[letternumber] )

single quoted

'example'

print (letter)

double quoted

"example"

numberletter = int(input( "how


many times to print letter " ))

Functions
print()

if numberletter >100:

display information on the screen

input()

display information from the user

int()

convert a value to an integer

len()

The length of the string

float()

Change number to be decimal number

str()

converts the value to a string

Comment, no effect

varlist = [myvar1,myvar2,myvar3]

print ( " too many letters


to print! " )
else:
print (letter *

print(random_var,varlist)
10 [1, 10, 100]
200 [2, 20, 200]
Bella ('Pin', 'Anpan', 'Bella')
2 [1, 10, 100, 2, 20, 200, 'Pin', 'Anpan', 'Bella']
3 [1, 2, 3]
bacon()
def bacon():
print("hello it's bacon")

numberletter )

print("line 2")

hello

print("line 3")

what is your first name?

print("line 4")

what is your last name?

return
bacon()

what is letter number?


how many times to print letter

bacon()
bacon()
hello it's bacon

List Random

line 2

import random

line 4

line 3

intlist = [1,10,100]

hello it's bacon

random_int =

line 2

random.choice(intlist)

line 3

print(random_int,intlist)
fplist = [2,20,200]
ramdom_fp = random.choice(fplist)
print(ramdom_fp,fplist)

line 4
hello it's bacon
line 2
line 3
line 4

strlist = ('Pin','Anpan','Bella')

By pinpin

Published 11th February, 2016.

Sponsored by CrosswordCheats.com

cheatography.com/pinpin/

Last updated 17th March, 2016.

Learn to solve cryptic crosswords!

Page 1 of 4.

http://crosswordcheats.com

Python Cheat Sheet

by pinpin via cheatography.com/25830/cs/6963/


myprintnew (text, decoration)

Vocabulary

def myprintnew (text, decoration):

Variable

Hold a value and can be changed

Print (2) integer

String

A list of characters such as

Print (2.5) floating point

number, letters, symbols

Print (Hello) string

Whole number/ counting number

Print (mystr) variable

print(decoration + str(text) +
decoration)
return

Integer

Example

myprintnew(1, "+++")

number

myprintnew('hello','-=-=-=-=-=-

Float

=-=-=')

number

myprintnew(1, "@@@@@@@")

Syntax

Grammar / Structure of language

Hi value can change

+++1+++

Modulo

Find the remainder

print (int(1.5)) 1

-=-=-=-=-=-=-=-=hello-=-=-=-=-=-=-=-=

Boolean

True / False

print (int(2)) 2

@@@@@@@1@@@@@@@
area of a triangle
def areaofTriangle(b, h):

Print (mystr,Hi,2,1.0) -- commas


The number in decimal

mystr name

Naming Conventions
Rules for naming variables:
- letters

area = 0.5 b h

- numbers

return area

- underscores (_)

user_base = float(input('Enter the


base of the triangle:'))
user_height = float(input('Enter

- can start with letters or underscores ONLY


- NO SPACES
Valid names:
- _mystr

the height of the triangle:'))

- my3

print('The area of the triangle

Hello_there

is',areaofTriangle(user_base,

Invalid names:

user_height))

- 3my= "hi" -- cannot start with number

def volumeofPrism(b,h,l):
volume = areaofTriangle(b, h)

mystr = Hi

- first name = "hi" -- no spaces allowed


- first-name -- dashes are not accepted

print (float(1)) 1.0 anything to


a float
Modulo/Remainder %
print (4%2) 0
print (30%7) 2
A radius of a circle code
#Ask the user for a radius of a circle
user_radius =input("What is the radius of the
circle?")
#Convert the given radius to a floating point
radius = float(user_radius)
#make a variable called pi
pi = 3.1415
#Calculate the area of the circle using
exponents
area = piradius*2

*l

#display the area of the circle to the user

return volume

print("The area of the circle is" ,area)

user_lenght = float(input('Enter

What is the radius of the circle?123

the length of the prism: '))

The area of the circle is 47527.753500000006

print('The volume of the prism


is',volumeofPrism(user_base,
user_height, user_lenght) )
Enter the base of the triangle:11111
Enter the height of the triangle:2222
The area of the triangle is 12344321.0
Enter the length of the prism: 3333
The volume of the prism is 41143621893.0
By pinpin

Published 11th February, 2016.

Sponsored by CrosswordCheats.com

cheatography.com/pinpin/

Last updated 17th March, 2016.

Learn to solve cryptic crosswords!

Page 2 of 4.

http://crosswordcheats.com

Python Cheat Sheet

by pinpin via cheatography.com/25830/cs/6963/


Reverse Code

_var1

maxvalue (cont)

word =input("Please enter your name: ")

_var1 = 1

print('The largest number

index = 0

_var1 = 3

is',max3(12222,5,10))

_var1 + 100

print('The largest number

print(_var1)

is',max3(12222,164.3415645,12134856

1240))

reverse =''
while index < len(word):
reverse = word[index]+ reverse
index = index + 1
print ("Reverse: ",reverse)
Please enter your name: Timmy
Reverse: ymmiT

The largest number is 3


maxlist
def maxlist(list):
maxvalue = list[0]

myprint(text)

The largest number is 12222


The largest number is 10
The largest number is 12222
The largest number is 121348561240

for item in list:

def myprint(text):

if item > maxvalue:

print("" + str(text) + " ")

maxvalue = mylist

return

return maxlist

myprint(1)

mylist =

myprint("hello")

[21365741,2135416,2,54131,1.1515]

myprint(2.5)

print(maxlist(mylist))

1
hello

maxvalue

2.5

def max2(num1,num2):
maxvalue = num1

areaOfCircle(r)

if num2 > maxvalue:

def areaOfCircle(r):

maxvalue = num2

if r <= 0:
return "Error: invalid

return maxvalue
print('The largest number
is',max2(2,3))

radius"
pi = 3.1415

print('The largest number

area = pi r * 2

is',max2(12222,10))

return area

def max3(num1,num2,num3):

Multiplication and Exponents


string * number

combine that string multiple


times

string * string

crash

number *

math - multiply

number
string ** string

crash

number **

math - multiply

number
string **

crash

number
Addition
string + string

combine together

string + number

crash

number + number

math-addition

Conditionals

user_radius = float(input("Enter

maxvalue = num1

the radius:"))

if num2 > maxvalue:

If.....

If the statement is true then do

print ('The area of the circle is',

maxvalue = num2

:then.....

command under then else do

areaOfCircle(user_radius))

if num3 > maxvalue:

else.......

command under else

Enter the radius:300


The area of the circle is 282735.0
Enter the radius:0
The area of the circle is Error: invalid radius

maxvalue = num3

return maxvalue
print('The largest number
is',max3(1,5,10))

By pinpin

Published 11th February, 2016.

Sponsored by CrosswordCheats.com

cheatography.com/pinpin/

Last updated 17th March, 2016.

Learn to solve cryptic crosswords!

Page 3 of 4.

http://crosswordcheats.com

Python Cheat Sheet

by pinpin via cheatography.com/25830/cs/6963/


Conditionals (cont)

Shoping List code

area of a triangle

while......

While this is true loop the command

shoppinglist = ['shoes', 'bags', 'shirts', 'pants']

def areaofTriangle(b, h):

under the conditiona

index = 0

While

loops forever

True
for each

For every item in the list repeat the

item in

command under the loop that many

name of

times. (a string is a list too)

list
Big or small code

area = 0.5 b h

while index < len(shoppinglist):


print (shoppinglist[index])
index = index + 1

return area
user_base = float(input('Enter the

for item in shoppinglist:

base of the triangle:'))

print (item)

user_height = float(input('Enter

shoes

the height of the triangle:'))

bags

print('The area of the triangle

shirts

is',areaofTriangle(user_base,

pants

user_height))

mystr = "hello THERE"

shoes

def volumeofPrism(b,h,l):

print (mystr.upper())

bags

print (mystr.lower())

shirts

print (mystr.capitalize())
print (mystr.title())

pants
printDefinitions

HELLO THERE
hello there

volume = areaofTriangle(b, h)
*l
return volume
user_lenght = float(input('Enter
the length of the prism: '))
print('The volume of the prism

Hello there

def printDefinitions(word):

Hello There

if word =="variable":
print ('....')

Please enter a number Code


user_number = input("Please enter a

elif word =="function":


print ('....')
elif word =="parameter":

number: ")
number = int(user_number)
countdown_string = ''
while number > 0:
countdown_string =
countdown_string+ str(number)

print ('....')

number = number-1
print (countdown_string)

print ('....')
elif word =="function call":

mystr = "Hello"
letter_num = 0
while letter_num < len(mystr):
print (mystr[letter_num])
letter_num = letter_num + 1

Enter the base of the triangle:11111


Enter the height of the triangle:2222
The area of the triangle is 12344321.0
Enter the length of the prism: 3333
The volume of the prism is 41143621893.0
doubleIt(number)
def doubleIt(number):

print ('....')

print ('....')
else:
print("unknown word")

Sort word per line

user_height, user_lenght) )

elif word =="argument":

elif word=="string":

is',volumeofPrism(user_base,

return
while True:
user_input = input("Enter word:
")
printDefinitions(user_input)

return number*2
print (doubleIt(3))
print (doubleIt(doubleIt(4)))
myvar = 12
myvar= doubleIt(myvar)
myvar= doubleIt(myvar)
print(myvar)
6
16
48

Enter word:

H
e
l
l
o
By pinpin

Published 11th February, 2016.

Sponsored by CrosswordCheats.com

cheatography.com/pinpin/

Last updated 17th March, 2016.

Learn to solve cryptic crosswords!

Page 4 of 4.

http://crosswordcheats.com

You might also like