0% found this document useful (0 votes)
71 views3 pages

Ppinkyy Phython-Note

This document provides a cheat sheet for Python concepts including: 1. Common functions like print, int, float, and str as well as basic operations like addition, multiplication, exponents, and modulo. 2. Naming conventions and examples of valid and invalid variable names. 3. A reverse word function example and converting numbers to binary. 4. Additional examples of code snippets and vocabulary terms.

Uploaded by

Openai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views3 pages

Ppinkyy Phython-Note

This document provides a cheat sheet for Python concepts including: 1. Common functions like print, int, float, and str as well as basic operations like addition, multiplication, exponents, and modulo. 2. Naming conventions and examples of valid and invalid variable names. 3. A reverse word function example and converting numbers to binary. 4. Additional examples of code snippets and vocabulary terms.

Uploaded by

Openai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

phython note Cheat Sheet

by ppinkyy via cheatography.com/25758/cs/6874/

funtion Naming Convention

print Show inform​ation on the screen Rule for giving name

int to tell that what you write is a number - letter


- numbers
float change number to be decimal number
- underscore _
str list of number, letter and symbol Valid name
- _myStr
Addition - my3
- Hello_​​there
str+str the result combine together
Invalid name
str+ int or float crash - 3my=”hi” -- cannot start with number

int/float + int/float math - addition - first name=”hi”


- first-name
- first+name
Multip​​li​c​ation and Exponents

string​*string crash Area of Circle


string​*number combine string together
"​"​"
number​*number math-m​ultiply Python Intro Assignment #2
number ** number math-e​xponent name
student number
string ** string crash
"​"​"
string​**n​umber crash #Ask the user for a radius of a circle
user_r​adius = input(​"What is a radius of a circle​?")
Reverse Word #Convert the given radius to a floating point
radius = float(​use​r_r​adius)
while True:
#Make a variable called pi
word = input(​"​Please enter a word")
pi = float(​3.1415)
index = 0
#Calculate the area of the circle using exponents
reverse = ' '
area = pi(rad​ius*2)
while int(index) < len(word):
#Display the area of the circle to the user
reverse = word[i​ndex] + (reverse)
print ("The area of the circle is", area)
index = int(index) + 1
print ("Re​verse: ", reverse)
example

Convert to binary Print (2) – integer


Print (2.5) – floating point
numb = int(in​put​('enter the number to convert to binary'))
Print (“Hello”) – string
bistring = ''
Print (mystr) – variable
while numb>0 :
rem = numb%2
bistring = str(re​m)+​bis​tring
numb = numb//2
print ('Binary Number : ', bistring)

By ppinkyy Published 15th February, 2016. Sponsored by ApolloPad.com


cheatography.com/ppinkyy/ Last updated 23rd February, 2016. Everyone has a novel in them. Finish Yours!
Page 1 of 3. https://apollopad.com
phython note Cheat Sheet
by ppinkyy via cheatography.com/25758/cs/6874/

example (cont) Vocabulary

Print (mystr​​,”​H​i​”,​​2,1.0) -- commas variable hold a value and can be change


mystr = “Hi” string a list of character such as number, letter and
mystr ← name
symbols
“Hi” ← value can change
integer a number without decimal
print (int(1.5)) → 1
print (int(“2”)) → 2 float number in dicimal
print (float(1)) → 1.0 anything to a float syntax structure of language
Modulo​​/R​e​m​ainder %
value returns a list of all the values available in a given
print (4%2) → 0
dictio​nary.
print (30%7) → 2
while it's a loop that will go over and over until you stop it

fuction loop tradit​ionally used when you have a piece of code


which you want to repeat n number of times
def myprint(text) :
input Run the program. In the Shell you should see
​ ​ ​ ​print ('xxx'​+st​r(t​ext​)+'​xxx')
myprint (1) print to show inform​​ation on the

modulo leave remainder


xxx 1 xxx
# Comment, no effect

Function (return number) Ex randon.choice program that someone wrote

import random to get get a random program from someone

def doubleIt (number): import to import program from someone

​ ​ ​ ​return number *2 import random + pick random item in the list


print (doubl​eIt(2)) random.ch​​oice()

print (doubl​eIt​('h​ello')) palindrome is derived from the Greek palínd​romos, meaning


myvar = double​It(​dou​ble​It(3)) running back again

print (myvar)
Guess word game
4
hellohello import random
12 #Create a list
guesslist = ['grape', 'orange', 'chlor​opl​ast', 'ribos​ome', 'lipst​ick']
chance = 3
symbol
score = 0
== equal

!= not equal

> greater than

< less than

>= greater than or equal to

<= less than or equal to

By ppinkyy Published 15th February, 2016. Sponsored by ApolloPad.com


cheatography.com/ppinkyy/ Last updated 23rd February, 2016. Everyone has a novel in them. Finish Yours!
Page 2 of 3. https://apollopad.com
phython note Cheat Sheet
by ppinkyy via cheatography.com/25758/cs/6874/

Guess word game (cont) fuction

print (guess​list) def bacon():


while chance != 0: ​ ​ ​ ​print ('bacon')
random​_item = random.ch​oic​e(g​ues​slist)
​ ​ ​ ​print ('egg')
user_input = input(​"​Please guess a word: ")
​ ​ ​ ​print ('text')
if user_input == random​_item:
​ ​ ​ ​print ('gta')
print ("That's correc​t!")
score = score + 100 ​ ​ ​ ​print ('guil​dwar')
print ("Sc​ore​:", score) ​ ​ ​ ​print ('maga​lodon')
else: ​ ​ ​ ​return
if user_input not in guesslist: bacon()
print ("Sorry, that isn't even in the list!")
bacon()
chance = chance - 1
print ("Chance Remain​ing​:", chance) bacon
else: egg
print ("Sorry, wrong choice​!") text
chance = chance - 1 gta
print ("Chance Remain​ing​:", chance) guildwar
if chance == 0: magalodon
print ("The word was", random​_item) bacon
print ("The score is", score) egg
text
Sort word per line gta
guildwar
mystr = "​Hel​lo" magalodon
letter_num = 0
while letter_num < len(my​str):
Example
print (mystr​[le​tte​r_num])
letter_num = letter_num + 1 def circle​are​a(r):
pi = 3.1415
H
area = pir*2
e
return area
l
r = input ('enter the radius of the circle : ')
l
r = float(r)
o
print ('the area of the circle is', circle​are​a(r))

Print Name enter the radius of the circle : 5


the area of the circle is 78.537​500​000​00001
name = "tim GIRARD​"
>>>
print (name.u​pp​er()) → TIM GIRARD
print (name.l​ow​er()) → tim girard
Example
print (name.c​ap​ita​lize()) → Tim girard
print (name.t​it​le()) → Tim Girard def circle​are​a(r):
pi = 3.1415
area = pir*2
return area
r = input ('enter the radius of the circle : ')
r = float(r)
print ('the area of the circle is', circle​are​a(r))

enter the radius of the circle : 5


the area of the circle is 78.537​500​000​00001
>>>

By ppinkyy Published 15th February, 2016. Sponsored by ApolloPad.com


cheatography.com/ppinkyy/ Last updated 23rd February, 2016. Everyone has a novel in them. Finish Yours!
Page 3 of 3. https://apollopad.com

You might also like