Show Menu
Cheatography

Loop Positive Integer

even = 0
odd = 0
while True:
user_input = int(in­put­("Enter a number :"))
user = user_input % 2
if user_input > 0:

if user == 0:
even = even + 1
elif user != 0:
odd = odd + 1


print(­use­r_i­nput)

else:
print ("Even number = ", even)
print ("Odd number = ", odd)
break

Fibonacci

num1 = 0
num2 = 1
fibonacci = num1 + num2
output = "­0,1­"
while fibonacci < 50:
output = output + "­," + str(fi­bon­acci)
num1 = num2
num2 = fibonacci
fibonacci = num1 + num2
print (output)

Fibonacci

num1 = 0
num2 = 1
fibonacci = num1 + num2
output = "­0,1­"
while fibonacci < 50:
output = output + "­," + str(fi­bon­acci)
num1 = num2
num2 = fibonacci
fibonacci = num1 + num2
print (output)

Variable Name

Good Variable Name

my_string = "­123­"
_hello = "­1"
mystring = 1
value1 = 1

#can have integers, lowerc­ase­/up­per­case, unders­cores
#the first character must be a lowerc­ase­/up­percase or an underscore

Bad Variable Name

email@ = 2
1value = 2

Upper Lower List

mystr = "­hello THERE"

print (mystr.up­per()) #Upper case all the letter in a word
print (mystr.lo­wer()) #Lower case all the letter in a word
print (mystr.ca­pit­ali­ze()) #Capital only first letter of first word
print (mystr.ti­tle()) #Capital first letter of every word.

#List in python

shoppi­nglist = ['Dogs', 'Cats', 'Mouses', 'Giraffe']

print(­sho­ppi­ngl­ist[2]) #Will print 'Mouses'

#while loop

item_n­umber = 0
while item_n­umber < len(sh­opp­ing­list):
print ("list item:", shoppi­ngl­ist­[it­em_­num­ber])
item_n­umber = item_n­umber + 1

#for loop

other = 0
for cat in shoppi­nglist:
other = other + 1
# print ("List item:", cat)
print (other)

Calculator

def calc(num1, num2, operat­ion):
if operation == "­sum­":
return sum(num1, num2)
elif operation == "­pro­duc­t":
return produc­t(num1, num2)
elif operation == "­dif­f":
return diff(num1, num2)
elif operation == "­div­":
return div(num1, num2)
else:
print ("Un­known Operat­ion­")


def sum(a, b):
return a + b


def product(a, b):
return a * b


def diff(a, b):
return a - b


def div(a, b):
if b == 0:
return ("Error: Undefined value")
else:
return a // b


print(­cal­c(12, 12, "­sum­"))
print(­calc(9, 18, "­dif­f"))
print(­cal­c(20, 10, "­pro­duc­t"))
print(­cal­c(12, 4, "­div­"))

Guessing Game

#PWTK 1002
scores = 0
chances = 3
while chances > 0:
print ("-=­-=-­=-=­-=-­=-=­-=-­=-=­-Gu­essing Game-=­-=-­=-=­-=-­=-=­-=-­-=-­=-=­-")
import random


mylist = ['apple', 'banana', 'papaya', 'melon', 'orange', 'grape', 'mango']

print (mylist)

random­_item = random.ch­oic­e(m­ylist)

user_guess = input(­"­Guess a word: ")


if user_guess == random­_item:
print ("That's correc­t")
scores = scores + 100
print ("Scores =", scores)
else:
chances = chances - 1
print ("Ch­ances left: ", chances)
if user_guess in mylist:
print ("That's incorr­ect­")
else:
print ("Sorry, that is not even in the list!")

if chances == 0:
print ("The word was: ", random­_item)
print ("Final score =", scores)
print ("GAME OVER!!­!!!­!!!­")

Vocabulary

String
A list of characters such as numbers, letters, symbols
Variable
Holds a value and can be changed
Syntax
The set of rules that defines the combin­ations of symbols
Boolean
Identified True or False (true is not the same as True, false is not the same as False)
Modulo
Finds the remainder after division of one number by another

Radius of a Circle

while True:

#Ask the user for a radius of a circle

user_r­adius = input(­"What is the radius of the circle ")

#Convert the given radius to a floating point

radius = (float­(us­er_­rad­ius))

#Make a variable called pi

pi = 3.1415

#Calculate the area of the circle using exponents

area = (pi (radius * 2))

#Display the area of the circle to the user

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

Basic Info

Basic Python Progra­mming Language
:(colon) = syntax
Syntax (=) Grammar
Variable = Something that cahnge­s(n­umbers, words)
Number vs Strings: my var = 1 + 2
print my var = 3
my var 2 = "­1" + "­2"
print my var "­12"
hello= "­hel­lo" + "It's me"
print hello = "­hel­loI­t's­me"
If 1==2:
When you do division in progra­mming the program will add decimal even if it doesn't have the decimal EX: 10.0
my var = "­you­rna­me"[0] (the first letter in progra­mming is 0 not 1)
== equal to
!= not equal to
> Greater than
>= Greater than or equal to
<= Less than or equal to
< Less than


print (len(f­ull­name))

if 1 == 2:
print ("tr­ue")
else:
print ("fa­lse­")

if 2 == 2:
print ("tr­ue")
else:
print ("fl­ase­")
print ("fa­lse­2")

Palindrome

def isPali­ndr­ome­(word):
reverse = "­"

for item in user_word:
reverse = item + reverse

revers­e_item = reverse

if revers­e_item == user_word:
return True#(­rev­ers­e_item, ("is a palind­rom­e"))
else:
return False#­(re­ver­se_­item, ("is not a palind­rom­e"))


while True:
user_word = input(­"­Enter a word: ")
length = len(us­er_­word)
if user_word == 'quit':
break
else:
print (length)

numlen = 0

while numlen < length / 2 + 1:
if user_w­ord­[nu­mlen] != user_w­ord­[-n­uml­en-1]:
print (user_­wor­d,"is not a palind­rom­")
break
numlen += 1
else:
print (user_­wor­d,"is a palind­rom­e")

Return Max Number

def max2(num1, num2):

maxvalue = num1

if num2 > maxvalue:
masvalue = num2

return maxvalue


def max3(num1, num2, num3):

maxvalue = num1

if num2 > maxvalue:
maxvalue = num2

if num3 > maxvalue:
maxvalue = num3

return maxvalue


def maxlis­t(l­ist):

maxvalue = list[0]
for num in list:
if num > maxvalue:
maxvalue = num

return maxvalue

print (maxli­st(­[1,­2,3­,4,5]))

Binary

while True:
user_n­umber = input(­"Put the number: ")

number = int(us­er_­number)

binary­_string = ''
while (number > 0):
remainder = (number % 2)
binary­_string = str(re­mai­nder) + binary­_string
number = (number // 2)

print ("binary string is", binary­_st­ring)

Loop Range

#Creating List

mylist = [1,2,3­,4,5,6]

mylist2 = ['hi', 'hello', 'anyth­ing']

mylist3 = [1, 'hello', 2.5]

print (mylist)
print (mylist2)
print (mylist3)

#How to make a list with all numbers from 0-10

mynumbers = range(11) #0-10 (Number starts with 0)

for num in mynumbers:
print (num)

mylist­2.a­ppe­nd(­'an­other item') #Adding item in a list
print (mylist2)

Reverse

word = input(­"­Input a word: ")
reverse = "­"

letter_num = 0

'''
while letter_num < len(word):
reverse = word[l­ett­er_num] + reverse
letter_num = letter_num + 1
'''

for item in word:
reverse = item + reverse

print ("Re­verse: "­,re­verse)

Command

# Hashtag
Add comment
# CAN WRITE ANYTHING HEREEE
''' (3 Apostr­ophe)
Long comment
''' ALSO HEREEEEE '''
print
To display something
print (var)
'' ''
Assign something in a variable
mystr = ("Ge­org­e")
int()
Set the number to interger
integer = int(20) #with no decimal
str()
Convert a variable to string
String = str(in­teger)
input()
Gain inform­ation from the user
Name = input(­" Put your name here: "
float()
Convert the number with decimal
Num = float(2) #the answer will be 2.0
len()
Find the length of the string
num1 = ("Ge­org­e"),, num2 = len(num1) #Answer will be 6
 

List Random­Choice

import random


intlist = [1, 2, 3, 4, 5]
random_int = random.ch­oic­e(i­ntlist)
print (intlist, random­_int)


fplist = [2.2, 3.5, 4.8, 6.2, 7.9]
random_fp = random.ch­oic­e(f­plist)
print (fplist, random_fp)

strlist = ['burger', 'cheese', 'ham', 'bacon', 'sandw­ich', 'pizza']
random_str = random.ch­oic­e(s­trlist)
print (strlist, random­_str)

mylist = [4, 6, 8, 11.4, 12.8, 17.6,'­coco', 'latte', 'mocha']
random­_item = random.ch­oic­e(m­ylist)
print (mylist, random­_item)

myvar1 = 1
myvar2 = 2
myvar3 = 3
varlist = [myvar1, myvar2, myvar3]
random_var = random.ch­oic­e(v­arlist)
print (varlist, random­_var)

Countdown

user_n­umber = input(­"­Please enter a number: ")

number = int(us­er_­number)

countd­own­_string = "­"

while number > 0:
countd­own­_string = countd­own­_string + str(nu­mber)
number = number - 1
#add the number to the string
#subtract 1 from the number

print (count­dow­n_s­tring)

Area of Triangle

# Pom Wintakorn 1002

def areaof­Tri­ang­le(­base, height):
return 0.5 base height

user_base = float(­inp­ut(­"­Enter the base of the triangle: "))

user_h­eight = float(­inp­ut(­"­Enter the height of the triangle: "))

print ("The area of the triangle is", areaof­Tri­ang­le(­use­r_base, user_h­eight))

area = areaof­Tri­ang­le(­use­r_base, user_h­eight)

def volume­ofP­ris­m(area, height):
return area * height

user_h­eight2 = float(­inp­ut(­"­Enter the second height of the triangle: "))

print ("The volume of the triangular prism is", volume­ofP­ris­m(area, user_h­eig­ht2))

Hexade­cimal

while True:
user_n­umber = input(­"Put the number: ")

number = int(us­er_­number)

#Loop the command

hex_string = ''
while (number > 0):
remainder = number % 16
if remainder == 10:
remainder = 'A'
elif remainder == 11:
remainder = 'B'
elif remainder == 12:
remainder = 'C'
elif remainder == 13:
remainder = 'D'
elif remainder == 14:
remainder = 'E'
elif remainder == 15:
remainder = 'F'
hex_string = str(re­mai­nder) + hex_string
number = number // 16

print ("he­xad­ecimal string is 0x"+ hex_st­ring)

Loop Review

#While loop
mylist = [1,2,3]

index = 0 #set to 0 because that is the first item in the list

while index < len(my­list):
print (mylis­t[i­ndex])
index = index + 1

#For loop
for item in mylist:
print(­item)

List Practice

import random


intlist = [1, 2, 3, 4, 5]
random_int = random.ch­oic­e(i­ntlist)
print (intlist, random­_int)


fplist = [2.2, 3.5, 4.8, 6.2, 7.9]
random_fp = random.ch­oic­e(f­plist)
print (fplist, random_fp)

strlist = ['burger', 'cheese', 'ham', 'bacon', 'sandw­ich', 'pizza']
random_str = random.ch­oic­e(s­trlist)
print (strlist, random­_str)

mylist = [4, 6, 8, 11.4, 12.8, 17.6,'­coco', 'latte', 'mocha']
random­_item = random.ch­oic­e(m­ylist)
print (mylist, random­_item)

myvar1 = 1
myvar2 = 2
myvar3 = 3
varlist = [myvar1, myvar2, myvar3]
random_var = random.ch­oic­e(v­arlist)
print (varlist, random­_var)

Number and String

"­Str­ing­" + "­Str­ing­"
Put both string together
Number + "­Str­ing­"
CRASH!
Number + Number
Additi­on(­Math)
"­Str­ing­" * "­Str­ing­"
CRASH!
"­Str­ing­" * Number
Print that string that number times
Number * Number
Multip­lic­ati­on(­Math)
String ** String
CRASH!
String ** Number
CRASH!
Number ** Number
Expone­nt(­Math)

Mathem­atics

+
Addition
-
Subtra­ction
*
Multip­lic­ation
/
Division (Result with floating point)
//
Division
**
Exponent
%
Modulo (Find remainder)
==
Equal to
>=
Greater than or equal to
<=
Less than or equal to
!=
Not equal to
<
Less than
>
More than

Multip­lic­ation Table

def multip­lic­ati­onT­able():
innum = int(in­put­("Enter a number: "))
for i in range(­1,11):
output = innum*i
print (str(i­nnum) + "­*" + str(i) + "­=" + str(ou­tput))



multip­lic­ati­onT­able()
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.