Basiccode
print() |
Show information that you want on the screen |
input() |
Gain information from user |
len() |
The length of the string |
int() |
Change number to be number integer |
float() |
Change number to be decimal number |
str() |
A list of number, letter and symbols |
# |
Comment |
Vocab
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 point |
The number in decimal |
syntax |
Grammar/Structure of lauguage |
Modulo |
Find the remainder |
Boolean |
True/False |
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
|
Sort per line
mystr = "Hello"
letter_num = 0
while letter_num < len(mystr):
print (mystr[letter_num])
letter_num = letter_num + 1
H
e
l
l
o
|
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 |
!= |
no equal to |
< |
less than |
> |
more than |
<= |
less than or equal |
>= |
more than or equal to |
% |
modulo, find the remainder |
Addition
string + string |
Combine together |
string + number |
crash |
number + number |
addition |
Multiplication
string * number |
combine that string multiple times |
string * string |
crash |
number * number |
multiply |
string ** string |
crash |
number ** number |
exponents |
string ** number |
crash |
Naming Convertion
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
|
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)
|
|
|
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
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)
|
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 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)
|
random.choice
import random
intlist = [9,8,7,6,5,4]
random_int = random.choice(intlist)
print (intlist, random_int)
fplist = [0.2,0.3,0.3]
random_fp = random.choice(fplist)
print (fplist, random_fp)
strlist = ["ABC", "BCA", "CAB"]
random_str = random.choice(strlist)
print (strlist, random_str)
mylist =[1,3,6,12,"ABC", "DEF", "HIJ"]
random_item =random.choice(mylist)
print (mylist, random_item)
myvar1 = 1
myvar2 = 2
myvay3 = 3
varlist =[myvar1, myvar2, myvar3]
random_var = random.choice(varlist)
print (varlist, random_var)
|
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment