COMP1531
1.4 - Python - Intro
Python
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.
Python
CLI (Command line interface)
- Can be run inline
- Can be run as cli entry
- Can be run via a file
Python
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}")
- Garbage collection
- More info on data types
Python - Basics
Strings (basics2.py)
sentence = "My"
sentence = sentence + " name is"
sentence += " Pikachu"
print(sentence)
print("Hi!!" * 10)
Python strings are immutable
Python - Basics
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")
.
Python - Basics
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])
Python - Basics
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}")
- lists are mutable, tuples are immutable
Python - Basics
Versions
python2
python3
Python
- Rapidly build applications due to high level nature
- Very straightforward toolchain to setup and use
- It's very structured compared to other scripting languages
- Useful in data science and analytics applications
Why Python?
COMP1531 21T1 - 1.4 - Python - Intro
By haydensmith
COMP1531 21T1 - 1.4 - Python - Intro
- 448