Interpreted V compiled
C program
Python program
Machine Code
Output
Output
Compile
Run
Compile & Run
Unlike C, Python is a very broad language with a rich range of libraries.
It's simply not possible for us to give you 100 lecture slides to teach you everything you need to know. For many of you, this will be the first course that you have to learn to take the basics from us, and then engage in self-learning to refine your knowledge of the language and it's uses.
CLI (Command line interface)
Basic code (basics1.py)
name = "Giraffe"
age = 18
height = 2048.11 # mm
num1 = 3 ** 3
num2 = 27 // 3
print(name + ", " + str(age) + ', ' + str(height))
print(name, age, height, sep = ', ')
print(f"{name}, {age}, {height}")
print(type(name))
print(type(age))
print(type(height))
print(f"3 ** 3 == {num1}")
print(f"27 // 3 == {num2}")
Strings (basics2.py)
sentence = "My"
sentence = sentence + " name is"
sentence += " Pikachu"
print(sentence)
print("Hi!!" * 10)
Python strings are immutable
Control structures, argc/argv (basics3.py)
import sys
argc = len(sys.argv)
empty = True
if argc > 0:
empty = False
if not empty:
if argc == 2:
print("Nearly there")
elif argc == 3:
if sys.argv[1] == "H" and sys.argv[2] == "I":
print("HI to you too")
else:
pass
else:
print("Please enter two letters as command line")
.
Lists, loops (basics4.py)
names = [ "Hayden", "Rob", "Isaac" ]
names.append("Vivian")
for name in names:
print(name)
print("===")
names += [ "Eve", "Mia" ]
for i in range(0, len(names)):
print(names[i])
Tuples (basics5.py)
x = 5
y = 6
point = (x, y)
print(point)
a, b = point # destructuring
print(f"{a}, {b}")
names = [ "Giraffe", "Llama", "Penguin" ]
for id, name in enumerate(names):
print(f"{id} {name}")
Versions
python2
python3