Python syntax refers to the set of rules that defines the combinations of symbols that are considered to be correctly structured programs in the Python language.
print("Hello, World!")
Python syntax
in the REPL (Read-Eval-Print-Loop) we can type the following
print("Hello, World!")
In the above program we use printfunction to print the message
The arguments for the function are whatever is inside the paretheses ()
Python syntax
Comments are the text that we can add to our program to make our code easier to understand
The computer ignores comments
# This program prints the string 'Hello World'
print("Hello, World!")
"""
We can also have longer multi
line comments using the triple quotes
"""
Python variables
Variables are names for storing data values
A variable is created the moment you first assign a value to it
x = 150
y = "Hello"
print(x)
print(y)
Python variables
Python will try to figure out what type (more later) the variable is (unlike other languages)
We will be talking about variables later!
value = input("Enter the value")
print(value)
Python libraries
With a computer we can do everything
But we do not want to create entire worlds from scratch
We can leverage the work of other people through Python libraries
Python libraries
We use import keyword for importing the library
We let Python know we are using the code from the library
Import turtle graphics:
import turtle
Python libraries
Various libraries do different things for example random number generation
import random
print(random.randint(1, 100))
This will print a random number between 1 and 100
What does the . mean
in the previous example we imported random
this is a python module that contains functions for generating random numbers
the . is used to access the functions in the module
we are basically saying "use the randint function from the random module"
Python libraries
There are two ways to import a library
import random
from random import *
we tend to use the first method as it is more explicit and can avoid complications later.
It would also bring in all of random and we don't need that
this is why we need module name . function name
Python libraries
Python graphics libraries:
Pillow: image manipulation with Python
Turtle: turtle graphics
Matplotlib: data visualisation
PyGame: creating games with Python
PyOpenGL: OpenGL bindings for Python
Creating an empty image
Now put everything we discussed about the syntax together
What is happening here?
#!/usr/bin/env pythonfrom PIL import Image
# by convention we use uppercase for constants
WIDTH = 400
HEIGHT = 300
img = Image.new(mode="RGB", size=(WIDTH, HEIGHT), color=(209, 123, 193))
img.show()
Time to play
create an empty file called first_image.py
copy the previous code into the file
now change things and see what happens, if it breaks ask for help (or try to fix it yourself)
Conclusion
What have you learned today
How to work with files and directories in Linux
How to run scripts from .py files
How to create an empty image
Homework
Modify the code to create an empty image of a yellow colour
Next time
What will you learn next time
Introduction to algorithmic thinking and how to go with the program flow