int
(integer numbers), float
(real numbers), str
(strings = words)list
(sequence of elements), tuple
(constant list), dictionary
(sequence of key/value pairs)# Create int, float and string
year = 2019
pi = 3.14
name = 'Mirek'
# Print the created variables
print(year,pi,name)
2019 3.14 Mirek
# Print the types of created variables
type(year),type(pi),type(name)
(int, float, str)
# Create two lists (with square brackets)
a = [1,2,3]
b = ['red','blue']
# Create one tuple (with round brackets)
c = ('The answer is', 42)
# Print the whole lists and tuples
print(a)
print(b)
print(c)
[1, 2, 3] ['red', 'blue'] ('The answer is', 42)
# Access individual elements (always with square brackets)
print(a[0], a[0]+10, a[0]+a[1])
print(c[0],c[1])
1 11 3 The answer is 42
# Print the types of our variables
type(a), type(b), type(c)
(list, list, tuple)
# Create a dictionary (with curly brackets)
d = {'H':1,'He':2,'Li':3}
# Print the whole dictionary
print(d)
{'H': 1, 'He': 2, 'Li': 3}
# Access individual elements (always with square brackets)
d['Be'] = 4
d['B'] = d['Be'] + 1
print(d)
{'H': 1, 'He': 2, 'Li': 3, 'Be': 4, 'B': 5}
# Print the type of our variable
type(d)
dict
# This value can be changed by user...
a = 10
# Condition (elif and/or else statemens can be omitted)
if (a > 0):
print('Number is positive')
elif (a == 0):
print('Number is zero')
else:
print('Number is negative')
Number is positive
# Basic syntax...
for b in (1,2,3):
print(b, end=' ')
1 2 3
# Slightly more complex example - temperature conversion table
# Convert degrees of Celsius to degrees of Kelvins, Fahrnheit and Reaumur
# (1) Print simple header of the table
print('%5s %7s %6s %6s' % ('T[C]','T[K]','T[F]','T[R]'))
print('-'*28)
# (2) Calculate and print conversion table
for x in range(0,21,2):
deg_C = x
deg_K = x + 273.15
deg_F = x * 9/5 + 32
deg_R = x * 0.8
print('%5.1f %7.2f %6.1f %6.1f' % (deg_C, deg_K, deg_F, deg_R))
T[C] T[K] T[F] T[R] ---------------------------- 0.0 273.15 32.0 0.0 2.0 275.15 35.6 1.6 4.0 277.15 39.2 3.2 6.0 279.15 42.8 4.8 8.0 281.15 46.4 6.4 10.0 283.15 50.0 8.0 12.0 285.15 53.6 9.6 14.0 287.15 57.2 11.2 16.0 289.15 60.8 12.8 18.0 291.15 64.4 14.4 20.0 293.15 68.0 16.0
# Basic syntax...
c = 0
while c < 10:
c = c + 1
print(c, end=' ')
1 2 3 4 5 6 7 8 9 10
# Slightly more complex example - greatest common divisor = GCD
# Euclid's algorithm
# (1) Insert two natural numbers (integers > 0)
a = 32
b = 40
# (2) Calculate GCD according to Euclid's algorithm
while (a != b):
if (a>b):
a = a-b
else:
b = b-a
# (3) Print the result
print(a)
8
n! = 1 * 2 * ... * n
3! = 1 * 2 * 3 = 6
# Define factorial
def factorial(n):
factorial = 1
for i in range(1,n+1):
factorial = factorial * i
return(factorial)
# Calculate factorial for a few numbers
for n in range(6):
print('n = %-3d n! = %-6d' % (n, factorial(n)))
n = 0 n! = 1 n = 1 n! = 1 n = 2 n! = 2 n = 3 n! = 6 n = 4 n! = 24 n = 5 n! = 120
F(0)=0, F(1)=1, F(n)=F(n-1)+F(n-2)
0,1,1,2,3,5,8...
# Print Fibonacci numbers up to F(n)
# (straightforward, step-by-step algorithm
# n = number of Fibonacci nubmers, can be changed
n = 20
# fn2,fn1 = initial values of F(n-2),F(n-1)
fn2,fn1 = 0,1
# print initial Fibonnaci numbers
print('%d,%d' % (fn2,fn1), end=',')
# calculate and print remaining Fibonacci numbers
for i in range(n-2):
fn = fn1 + fn2
fn2 = fn1
fn1 = fn
print(fn, end=',')
# print final elipsis = ...
print('...')
0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,...
# Alternative, slightly shorter and a bit more clever algorithm
# (taken and adjusted from Python tutorial
n = 20
a,b = 0,1
for i in range(n):
print(a,end=',')
a,b = b,a+b
print('...')
0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,...
from math import sqrt
# Input coefficients a,b,c
# (this part can be changed by user
a = 1
b = 0
c = -4
# Calculate discriminant
d = b**2 - 4*a*c
# Calculate roots of quadratic equation
print('Equation to solve: a*x**2 + b*x + c = 0')
print('Coefficients (a, b, c): %d, %d, %d ' % (a,b,c))
print('----')
# ...special cases (a,b == 0), which would cause errors...
if (a == 0):
if (b == 0):
print('Special case (a=0,b=0) => nonsense, no solution.')
else:
x = -c/b
print('Special case (a=0) => linear equation: x =',x)
# ...other cases, quadratic equation with (a,b != 0)
else:
if (d > 0):
x1 = (-b + sqrt(d)) / (2*a)
x2 = (-b - sqrt(d)) / (2*a)
print('Quadratic equation, two real solutions:', x1, x2)
elif (d == 0):
x = -b / (2*a)
print('Quadratic equation, one real solution:', x)
else: # (d < 0)
print('Quadratic equation, no real solution.')
Equation to solve: a*x**2 + b*x + c = 0 Coefficients (a, b, c): 1, 0, -4 ---- Quadratic equation, two real solutions: 2.0 -2.0