Show Menu
Cheatography

Function

print
Show inform­ation that you want on the screen
int
Change number to be number integer
float
Change number to be decimal number
input
Gain inform­ation from user
str
A list of number, letter and symbols
len
The length of the string
#
Comment, no effect

Addition

string + string
Combine together
string + number
CRASH!
number + number
Additi­on(­Math)

Example

 
Print (2) – integer
Print (2.5) – floating point
Print (“Hello”) – string
Print (mystr) – variable
Print (mystr­­,”­H­i­”,­­2,1.0) -- commas

mystr = “Hi”
mystr ← name
“Hi” ← value can change

print (int(1.5)) → 1
print (int(“2”)) → 2
print (float(1)) → 1.0 anything to a float

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

Area of Circle

 
"­­"­"
Python Intro Assignment #2
name
student number
"­­"­"

#Ask the user for a radius of a circle
user_r­­adius = input(­­"What is a radius of a circle­­?")

#Convert the given radius to a floating point
radius = float(­­us­e­r­_r­­adius)

#Make a variable called pi
pi = float(­­3.1­415)

#Calculate the area of the circle using exponents
area = pi(ra­­diu­­s*2)

#Display the area of the circle to the user
print ("The area of the circle is", area)
 

Multip­lic­ation and exponents

string­*number
Combine that string
string­*string
CRASH!
number­*number
Multip­ly(­math)
string­**s­tring
CRASH!
number­**n­umber
Expone­nt(­math)
string­**n­umber
CRASH!

Vocabulary

Variable
Hold a value and can be change
String
A list of character such as nubmer, letter and symbols
Interger number
Whole nubmer/ counting number
Float number
The numer in decimal
Syntax
Gramma­r/S­tru­cture of language
Modulo
Find the remainder
Boolean
True/False

Countdown machine

 
user_n­­umber = input(­­"What number do you want to count down? ")
number = int(us­­er­_­n­umber)
countd­­ow­n­_­string = ' '

while number > 0:
countd­­ow­n­_­number = countd­­ow­n­_­string + str(nu­­mber) + " "
number = number - 1
#print­­(n­u­mber)

print (count­­do­w­n­_s­­tring)

Sort word per line

 
mystr = "­­He­l­l­o"

letter_num = 0

while letter_num < len(my­­str):
print (mystr­­[l­e­t­te­­r_num])
letter_num = letter_num + 1
H
e
l
l
o

Sort fruit list

 
fruits = [] #an empty list

for number in range(5):
user_fruit = input(­­"­P­lease enter a fruit")
fruits.ap­­pe­n­d­(u­­ser­­_f­ruit)

print ("Size of fruit list is", len(fr­­uits))

fruits.sort()

for fruit in fruits:
print ("Fruit: ", fruit)
 

Convert to binary

 
user_n­­umber = ' '

while user_n­­umber != ' 0 ' :
user_n­­umber = input ("Enter a number to convert to binary­­")
number = int(us­­er­_­n­umber)
binary­­_s­tring = ' '

while (number > 0):
remainder = number%2
binary­­_s­tring = str(re­­ma­i­n­der)+ binary­­_s­tring
number = number//2

print ("Binary string is", binary­­_s­t­ring)

Naming Convention

 
Rule for giving name
- letter
- numbers
- underscore _

Valid name
- _myStr
- my3
- Hello_­­there

Invalid name
- 3my=”hi” -- cannot start with number
- first name=”hi”
- first-name
- first+name

Print Name

 
name = "tim GIRARD­­"

print (name.u­­p­p­er()) → TIM GIRARD
print (name.l­­o­w­er()) → tim girard
print (name.c­­a­p­i­ta­­lize()) → Tim girard
print (name.t­­i­t­le()) → Tim Girard

Math

==
equal to
!=
no equal to
<
less than
>
more than
<=
less than or equal to
>=
more than or equal to
%
Modulo, find the remainder

Reverse Word

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

while int(index) < len(word):
reverse = word[index] + (reverse)
index = int(index) + 1

print ("Reverse:",reverse)
 

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.