from IPython.display import HTML
import requests
HTML(requests.get("https://git.io/fh5WI").text)
3*5
print("hello!")
2+3
2*3
2**3
2.0*3.0
2**10000
2.0**10000
19%5
20%5
a = 3
b = 4
a+b
a,b,c = 4, 5, 6
a
b
c
a, b
a, b = b, a
a, b
Built-in functionality of Python is limited. E.g. no trigonometric functions:
sin(0)
import math
dir(math)
help(math)
math.pi
math.sin(math.pi)
import math as m
m.sin(m.pi)
from math import sin, pi
sin(pi)
from math import *
cos(pi)
log(pi)
a = 'Hello'
print(a)
b = "world!"
print(b)
c = '''more than
one line'''
print(c)
c = a + b
print(c)
print(10*a)
a = 1
b = 2
c = a + b
s = f"{a} plus {b} equals {c}"
print(s)
if condition:
do this
do this
do this
else:
do this
do this
x = 101
if x > 100:
print('big number')
print('Done!')
x = 0
if x > 100:
print("big number")
else:
print("small number")
print("Done!")
10 > 5
10 == 5
10 != 5
(10 > 5) or (10 < 5)
(10 > 5) and (10 < 5)
True
.False
.x = 7
if x:
print("Yes!")
else:
print("No!")
i = 1
while i < 5:
print(i*10)
i += 1
print("Done!")
r = input("Delete file? (yes/no): ")
while True:
if r == 'no':
print("Cancelled")
break
elif r == 'yes':
print("File deleted")
break
else:
print(f'invalid option: "{r}"')
r = input("Delete file? (yes/no): ")
mylist = [a, b, c]
mylist = [3, 5.1, "Hello", True]
mylist
for x in mylist:
print(2*x)
For all practical purposes True
and False
are the same as 1 and 0:
True == 1
False == 0
Accessing list items (indexing starts at 0):
mylist
mylist[0]
mylist[2]
Negative indexing counts from the end:
mylist[-1]
mylist[-2]
List concatenation:
x = [1, 2, 3] + ['a', 'b', 'c']
x
Appending an element to a list:
x
x.append('Hello')
x
mylist[start : stop]
mylist[start : stop : step]
numbers = list(range(20))
numbers
numbers[5:15]
numbers[5:15:3]
s = "University at Buffalo"
s[4:16]
s[::2]
[f(x) for x in mylist]
[f(x) for x in mylist if condition]
numbers = list(range(10))
numbers
squares = [x**2 for x in numbers]
squares
even_squares = [x**2 for x in numbers if x%2 == 0]
even_squares
The same with the "for" loop:
even_squares2 = []
for x in numbers:
if x%2 == 0:
even_squares2.append(x**2)
even_squares2
fb = [("Fizz" if x%3==0 else "") + ("Buzz" if x%5==0 else "") or x for x in range(1, 20) ]
for i in fb:
print(i)
x = 5
("Fizz" if x%3==0 else "")
x = 15
("Fizz" if x%3==0 else "") + ("Buzz" if x%5==0 else "")
Note: or
and and
return the value of the last expression they evaluate:
a = "hi"
b = "hello"
a or b
x = 10
x or 2/0
x = 15
("Fizz" if x%3==0 else "") + ("Buzz" if x%5==0 else "") or x
my_dictionary = {key1:value1, key2:value2, ...}
game_scores = {}
game_scores["John"] = 100
game_scores["Ann"] = 120
game_scores["Tom"] = 70
game_scores
game_scores["Ann"]
game_scores["Ann"] = 130
game_scores
Adding new records:
game_scores["Beth"] = 0
game_scores
for name in game_scores:
print(name, game_scores[name])
def add(x, y):
z = x+y
return z
add(2, 3)
add("hello ", "world!")
from time import sleep
def timer(seconds, message = "Time's up!"):
for t in range(seconds,0,-1):
print(t)
sleep(1)
print(message)
timer(10)
timer(5, message = "Boom!")
class Player:
name = None
score = 0
class Player:
name = None
score = 0
def bonus(self, percent):
self.score = int(self.score * (1 + percent/100))
def __str__(self):
return f"Name: {self.name}\nScore: {self.score}"
p = Player()
p.name = "John"
p.score = 10
print(p)
p.bonus(50)
print(p)
q = Player()
print(q)
q.name = "Beth"
q.score = 20
print(q)
dir(q)
[1, 2, 3].__class__
dir([1, 2, 3])
[1, 2, 3].__add__([4,5])