funtion
print |
Show information on the screen |
int |
to tell that what you write is a number |
float |
change number to be decimal number |
str |
list of number, letter and symbol |
Addition
str+str |
the result combine together |
str+ int or float |
crash |
int/float + int/float |
math - addition |
Multiplication and Exponents
string*string |
crash |
string*number |
combine string together |
number*number |
math-multiply |
number ** number |
math-exponent |
string ** string |
crash |
string**number |
crash |
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) |
Convert to binary
numb = int(input('enter the number to convert to binary'))
bistring = ''
while numb>0 :
rem = numb%2
bistring = str(rem)+bistring
numb = numb//2
print ('Binary Number : ', bistring) |
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 |
Area of Circle
"""
Python Intro Assignment #2
name
student number
"""
#Ask the user for a radius of a circle
user_radius = input("What is a radius of a circle?")
#Convert the given radius to a floating point
radius = float(user_radius)
#Make a variable called pi
pi = float(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) |
example
Print (2) – integer
Print (2.5) – floating point
Print (“Hello”) – string
Print (mystr) – variable
Print (mystr,”Hi”,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/Remainder %
print (4%2) → 0
print (30%7) → 2 |
fuction
def myprint(text) :
print ('xxx'+str(text)+'xxx')
myprint (1)
|
Function (return number) Ex
def doubleIt (number):
return number *2
print (doubleIt(2))
print (doubleIt('hello'))
myvar = doubleIt(doubleIt(3))
print (myvar)
|
|
|
symbol
== |
equal |
!= |
not equal |
> |
greater than |
< |
less than |
>= |
greater than or equal to |
<= |
less than or equal to |
Vocabulary
variable |
hold a value and can be change |
string |
a list of character such as number, letter and symbols |
integer |
a number without decimal |
float |
number in dicimal |
syntax |
structure of language |
value |
returns a list of all the values available in a given dictionary. |
while |
it's a loop that will go over and over until you stop it |
loop |
traditionally used when you have a piece of code which you want to repeat n number of times |
input |
Run the program. In the Shell you should see |
print |
to show information on the |
modulo |
leave remainder |
# |
Comment, no effect |
randon.choice |
program that someone wrote |
import random |
to get get a random program from someone |
import |
to import program from someone |
import random + random.choice() |
pick random item in the list |
palindrome |
is derived from the Greek palíndromos, meaning running back again |
Guess word game
import random
#Create a list
guesslist = ['grape', 'orange', 'chloroplast', 'ribosome', 'lipstick']
chance = 3
score = 0
print (guesslist)
while chance != 0:
random_item = random.choice(guesslist)
user_input = input("Please guess a word: ")
if user_input == random_item:
print ("That's correct!")
score = score + 100
print ("Score:", score)
else:
if user_input not in guesslist:
print ("Sorry, that isn't even in the list!")
chance = chance - 1
print ("Chance Remaining:", chance)
else:
print ("Sorry, wrong choice!")
chance = chance - 1
print ("Chance Remaining:", chance)
if chance == 0:
print ("The word was", random_item)
print ("The score is", score) |
Sort word per line
mystr = "Hello"
letter_num = 0
while letter_num < len(mystr):
print (mystr[letter_num])
letter_num = letter_num + 1 |
Print Name
name = "tim GIRARD"
print (name.upper()) → TIM GIRARD
print (name.lower()) → tim girard
print (name.capitalize()) → Tim girard
print (name.title()) → Tim Girard |
fuction
def bacon():
print ('bacon')
print ('egg')
print ('text')
print ('gta')
print ('guildwar')
print ('magalodon')
return
bacon()
bacon()
|
bacon
egg
text
gta
guildwar
magalodon
bacon
egg
text
gta
guildwar
magalodon
Example
def circlearea(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', circlearea(r)) |
enter the radius of the circle : 5
the area of the circle is 78.53750000000001
>>>
Example
def circlearea(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', circlearea(r)) |
enter the radius of the circle : 5
the area of the circle is 78.53750000000001
>>>
|
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment
More Cheat Sheets by ppinkyy