from IPython.display import IFrame, Math, Latex
IFrame('http://jupyter-notebook-beginner-guide.readthedocs.io/en/latest/what_is_jupyter.html', width='100%', height=350)
IFrame("https://wiki.python.org/moin/BeginnersGuide/Overview", width='100%', height=350)
IFrame('https://en.wikipedia.org/wiki/Object-oriented_programming', width='100%', height=350)
from IPython.display import YouTubeVideo
YouTubeVideo('-5kkdYO7QeQ')
Student_first_name = "Aramayis"
Student_surname= "Dallakyan"
Student_year_at_school = 3
Student_GPA = 3.85
pi = 3.14159
diameter = 11.2
area = pi*(diameter**2)
print(area)
" This is a string"
Student_first_name = "aramayis"
Student_surname= "dallakyan"
Student_full_name = Student_first_name + " " + Student_surname
print(Student_full_name)
print(Student_full_name.title())
.title()
is a method that tells Python to act on variable Student_full_name
titled_name =Student_full_name.title() #### You can create new variable and assingn titles string
print(titled_name)
print (Student_full_name.upper())
print(Student_full_name.lower())
add(+)
, subtract(-)
, multiply(*)
, and divide (/)
integers in Python.print(2+3)
$$ P= 4 * Edge$$
a = 5
Perimeter = 4*a
print ("Perimeter= " + str(Perimeter))
$$Area = (Edge)^2$$
Area_1 = a*a
##or
Area_2 = a**2
print ("Area1=" + str(Area_1))
print ("Area2=" + str(Area_2))
print("Are they equal?: "+ str(Area_1 == Area_2))
age = 29
message = "Happy" + age +"rd Birthday"
This is example of "type error". That is Python can't recognize the kind of information you're using
To be able use integers within string we need explicit specification.
age = 29
message = "Happy " + str(age) +"rd Birthday"
print(message)
Information = str(Student_year_at_school)+"rd year student" + " " +Student_full_name
print(Information)
Courses = ["AGEC 619", 'AGEC 630', 'AGEC 621',"AGEC 622"]
print (Courses)
print(Courses[0])
print(Courses[0].lower())
Remember: Index position starts from 0, Not 1
print (Courses[1])
print (Courses[5])
print (Courses[-1])
Information = "The required Courses are " + Courses[0].title() + ' and ' + Courses[3].title()
print (Information)
Courses[0]= 'ECON 629'
print(Courses)
Courses.append('AGEC 619')
print(Courses)
Other_Courses= []
Other_Courses.append("Stat 630")
Other_Courses.append("Stat 610")
Other_Courses.append("Econ 630")
print (Other_Courses)
Courses.insert(0,"MRKT 680")
print(Courses)
del Courses[1]
print(Courses)
Not_offered_course = Courses.pop()
print (Not_offered_course)
pop()
for any position.Other_Department_Course = Courses.pop(0)
print(Other_Department_Course.title() + " does not offered in fall")
remove()
method to remove element by values.print(Courses)
Courses.remove("AGEC 622")
print (Courses)
Courses= ["AGEC 619", 'AGEC 630', 'AGEC 621',"AGEC 622","ECON 629", "STAT 610", "MRKT 680", "MATH 641"]
print (Courses)
Courses.sort()
print (Courses)
Courses.reverse()
print(Courses)
len(Courses)
list
and perform the same task with each item. For example in a list
of numbers you want to perform the same statistical operation on every element.Students = ['Muhammad Abdullah',"Kayli Abernathy","Jacqueline Alvarez","Lainey Bourgeois","Garrett Carr","Chance Chapman"]
for student in Students:
print (student)
Python initially reads the first line of the loop.
This line tells Python to go into list Students and retrieve first element and store it in the variable "student". The first student is "Muhammad Abdullah". Then Python reads the next line:
Python prints the first value of "student". Because the list contains more values, PYTHON returns to the first line of the loop. PYTHON repeats the same purcedure until no more items remains in the list.
Note: for building loops you can use any name instead of student.
for student in Students:
print (student.title() +" is a good student")
names = str()
for student in Students:
names += student +" ,"
print("Our MAB students are " + names)
range()
function makes it easy to deal with numbers.for value in range(1,6):
print(value)
list
numbers = list(range(1,10))
print(numbers)
even_numbers = list(range(2,11,2))
print(even_numbers)
squares = list()
for number in range(1,11):
square = number**2
squares.append(square)
print(squares)
min(squares)
max(squares)
sum(squares)
for loop
code more concise and more efficient when dealing with lists. The answer is yes. We need to use list comprehension
.squares = [number**2 for number in range(1,11)]
print (squares)
slice
. Students = ['Muhammad Abdullah',"Kayli Abernathy","Jacqueline Alvarez","Lainey Bourgeois","Garrett Carr","Chance Chapman"]
print(Students[1:3])
print(Students[:3])
print(Students[2:])
Slice
print("Here are the firt four MAB students:")
for student in Students[:4]:
print (student)
IF
statement helps to exemine current state of a program and respond appropriately to that state.Students = ['Muhammad Abdullah',"Kayli Abernathy","Jacqueline Alvarez","Lainey Bourgeois","Garrett Carr","Chance Chapman"]
name= "Muhammad Abdullah"
if name in Students:
print (name + " is MAB student")
uppercase
.title case
.Important: for conditional statements you need to use "=="
equality operator, which returns True
if the values on the left and right side of the operator match, and False
if they don't.
requested_course = "Agec 619"
if requested_course != "Agec 622":
print ("Request Another Course")
!=
operator compares if requested_course
is equal to the value "Agec 622"
. If these two value do not match Python returns True
.########TRY TO ANSWER############
Question = 2*2
Answer = int(input("Enter an integer:"))
if Answer == 4:
print ("Good Job")
elif (Answer!=4) and (Answer <3) and (Answer <5):
print("You are close")
else:
print ("Try one more time **GENIOUS**")
x = int(input("Enter an integer" ))
for ans in range (0, abs(x)+1):
if ans**3 >= abs(x):
break
if ans**3 !=abs(x):
print (x, "is not a perfect cube")
else:
if x<0:
ans = -ans
print("Cube root of",x,'is ',ans)
"break" statement
causes the loop to terminate before it has been run on each element in the sequence over which it is iterating.student_1= {'UIN': "925024924",
'first_name': "Muhammad",
'second_name': "Abdullah"}
for key,value in student_1.items():
print("\nKey: " + key)
print("Value: " + value)
Students = ['Muhammad Abdullah',"Kayli Abernathy","Jacqueline Alvarez","Lainey Bourgeois","Garrett Carr","Chance Chapman"]
student_1= {'UIN': "925024924",
'first_name': "Muhammad",
'second_name': "Abdullah",
'courses':["Agec 619",'Agec 621','Agec 622']}
student_2= {'UIN': "925024924",
'first_name': "Kayli",
'second_name': "Abernathy",
'courses':["Agec 619",'Stat 610','Stat 630']}
student_3= {'UIN': "925024924",
'first_name': "Jacqueline",
'second_name': "Alvarez",
'courses':["Agec 621",'Math 641','Stat 611']}
Students = [student_1,student_2,student_3]
count =0
student_names= []
for student in Students:
if "Agec 619" in student['courses']:
student_names.append(student["first_name"])
count= count +1
print("Total number of Agec 619 students are: ",count)
print ("Student who take the class are", student_names)
####### OR we can be more clever
print("Total number of Agec 619 students are: ",len(student_names))
"for loop"
takes a collection of items and executes a block of code once for each item in the collection. In contrast, the while loop
runs as long as, a certain condition is true.current_number = 0
while current_number <10:
current_number +=1
if current_number % 2 ==0:
continue
print (current_number)
current_number = 0
while current_number <10:
current_number +=1
if current_number % 2 ==0:
continue
print (current_number)
current_number = 0
while current_number <10:
current_number +=1
if current_number % 2 ==0:
continue
print (current_number)
###Find c such that X**2 -24 is within epsilon of 0
epsilon = 0.01
k =24.0
guess = k/2.0
while abs(guess*guess -k) >= epsilon:
guess = guess - (((guess**2) - k)/(2*guess))
print ("Square root of",k,"is about",guess)
def
name of function(list of formal parameters) body of function
#########
def max(x,y):
"""Define the function which find the maximum between two numbers.
Return statement takes a value from inside a function and sends it back to the line that called the function."""
if x>y:
return x
else:
return y
max(3,4)
def new_person(first_name,last_name, age=""):
"""Function returns a dictionary information abour a person.
Note: Age value is optional"""
person = {'first':first_name,'last':last_name}
if age:
person['age']=age
return person
new_person("Aram","Dallakyan")
new_person("Aram","Dallakyan","29")
def findRoot(x,power, epsilon):
'''assumes pwr an int; val, epsilon floats > 0
Returns float y such that y**power is within epsilon of x
If such a float does not exist, it returns None'''
assert type(power) == int
assert type(x) == float
assert type(epsilon) == float
assert power > 0 and epsilon > 0
if power%2 and val < 0:
return None
low = min(-1,0,x)
high = max(1.0,x)
ans = (high + low)/2.0
while abs(ans**power -x) >= epsilon:
#print 'ans =', ans, 'low =', low, 'high =', high
if ans**power < x:
low = ans
else:
high = ans
ans = (high + low)/2.0
return ans
findRoot(4.0,2,0.01)
def readInt():
while True:
val = input("Enter an Integer: ")
try:
val = int(val)
return(val)
except ValueError:
print (val, "is not integer")
readInt()