Function
print() |
show information that you want on the screen |
int() |
change number to be number integer |
float() |
change number to be decimal number |
str() |
a list of number, letter and symbols |
len() |
the length of the string |
# |
comment (no effect) |
Multiplication and Explonants
string * number |
combine that string |
string * string |
CRASH |
number * number |
multiply (math) |
string ** string |
CRASH |
number ** number |
exponent (math) |
string ** number |
CRASH |
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) |
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) |
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)) |
|
|
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 |
Floating number |
the number in decimal |
Syntax |
grammar of python |
Modulo |
find the remainder |
Boolean |
true/false |
Addition
string + string |
combine together |
string + number |
CRASH |
number + number |
addition (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 |
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)
|
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 |
|
|
Math
== |
equal to |
!= |
not 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[i ndex] + (reverse)
index = int(index) + 1
print ("Re verse: ", reverse) |
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 |
|