0,01,012,0123,01234
mystring = ''"
count = 0
while count <= 4:
mystring = mystring + str(count)
print (mystring)
count = count + 1
mystring = ""
for num in range(5):
mystring = mystring + str(num)
print (mystring) |
Function
print() |
Show information that you want on the screen |
int() |
Change number to be number integer |
float() |
Change number to be decimal number |
input() |
Gain information from user |
str() |
A list of number, letter and symbols |
len() |
The length of the string |
# |
Comment, no effect |
Vocabulary
Variable |
Hold a value and can be change |
String |
A list of character such as number, letter and symbols |
Integer number |
Whole number/counting number |
Float number |
The number in decimal |
Syntax |
Grammar/Structure of lauguage |
Modulo |
Find the remainder |
Boolean |
True/False |
==
myboolean = 2 == 3
if myboolean:
print ("truth")
else:
print ("lies") |
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 |
Selecting Largest Value
def max2 (num1,num2):
if num1>num2:
return num1
if num1<num2:
return num2
def max3 (num1,num2,num3):
if num1>num2 and num1>num3:
return num1
if num2>num1 and num2>num3:
return num2
if num3>num1 and num3>num2:
return num3
num1=input("Enter your num1:")
num2=input("Enter your num2:")
num3=input("Enter your num3:")
print("the largest number of max3 is:",max3(num1,num2,num3))
print("the largest number of max2 is:",max2(num1,num2)) |
Sort word per line
mystr = "Hello"
letter_num = 0
while letter_num < len(mystr):
print (mystr[letter_num])
letter_num = letter_num + 1 |
Definition
def printDefinition(word):
if word == "variable":
print ("""
A variable is the the thing that can be changed.
""")
elif word == "parameter":
print ("""
A parameter is the limiting factor
""")
elif word == "argument":
print ("""
An argument is the identifier that you give to function
""")
elif word == "string":
print ("""
A string is something that can be repeated by the number.
""")
elif word == "function call":
print ("""
A function call is the word you use to reuse the function.
""")
else:
print ("unknown word")
while True:
user_input = input("Please type the word :")
printDefinition(user_input) |
|
|
Fibonacci
num1 = 0
num2 = 1
fibonacci = num1 + num2
output = "0,1"
while fibonacci < 50:
output = output +","+ str(fibonacci)
num1 = num2
num2 = fibonacci
fibonacci = num1 + num2
print (output) |
1 * 1 = 1
def multiplicationTable(num):
multi = 0
while multi < 10:
multi = multi + 1
user_output = num*multi
print ( num,"*",multi,"=",user_output)
user_num = int(input("Enter the number: "))
multiplicationTable(user_num) |
Hex
user_number = input("Enter number to convert to hex : ")
number = int(user_number)
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(remainder) + str(hex_string)
number = number // 16
print ("Hex string is 0x",hex_string) |
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) |
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 |
Boolean
False or True |
True |
False and True |
False |
True and False |
False |
True and True |
True |
False or False |
False |
Multiplication and Exponents
string * number |
Combine that string |
string* string |
CRASH! |
number * number |
Multiply (Math) |
string ** string |
CRASH! |
number ** number |
Exponent (Math) |
string ** number |
CRASH! |
Addition
string + string |
Combine together |
string + number |
CRASH! |
number + number |
Addition (Math) |
Math
== |
equal to |
!= |
no equal to |
< |
less than |
> |
more than |
<= |
less than or equal to |
>= |
more than or equal to |
% |
Modulo, Find the remainder |
|
|
Even,Odd number
even = 0
odd = 0
while True:
user_num = int(input("Enter the number :"))
if user_num >= 0:
if user_num % 2 == 0:
even = even + 1
else:
odd = odd + 1
else:
print ("Even number :", even)
print ("Odd number :", odd)
break |
For loop word
For word in mylist:
print (word) |
Guess
import random
chance = 3
score = 0
mylist = ['Hack', 'ToeyD.', 'Patter','Tim','Lily']
random_item = random.choice(mylist)
while chance > 0:
print (mylist)
print ("Chances Remaining =",chance)
guess = input("Guess a word from the above :")
if guess == random_item:
score = score + 100
print ("That's correct!","The score is :",score)
random_item = random.choice(mylist)
else:
print ("Sorry, wrong choice!")
chance = chance - 1
if guess in mylist:
print ("")
else:
print ("Sorry,that is not even in the list!")
if chance == 0:
print ("Game Over! The word was",random_item)
print ("Final score: ",score) |
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 |
Sort fruit list
fruits = [] #an empty list
for number in range(5):
user_fruit = input("Please enter a fruit")
fruits.append(user_fruit)
print ("Size of fruit list is", len(fruits))
fruits.sort()
for fruit in fruits:
print ("Fruit: ", fruit) |
Countdown Machine
user_number = input("What number do you want to count down? ")
number = int(user_number)
countdown_string = ' '
while number > 0:
countdown_number = countdown_string + str(number) + " "
number = number - 1
#print(number)
print (countdown_string) |
Convert to binary
user_number = ' '
while user_number != ' 0 ' :
user_number = input ("Enter a number to convert to binary")
number = int(user_number)
binary_string = ' '
while (number > 0):
remainder = number%2
binary_string = str(remainder)+ binary_string
number = number//2
print ("Binary string is", binary_string) |
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) |
|
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets