COMPUTER PROGRAMMING 2
introduction
to python
(Indention, Comments, Variables)
Instructor: Louie Jay L. Haloc
what is python?
Python is a popular programming language. It was created by
Guido van Rossum, and released in 1991.
It is used for:
web development (server-side),
software development,
mathematics,
system scripting.
wHAT CAN PYTHON DO?
Python can be used on a server to create web applications.
Python can be used alongside software to create workflows.
Python can connect to database systems. It can also read and
modify files.
wHAT CAN PYTHON DO?
Python can be used to handle big data and perform complex
mathematics.
Python can be used for rapid prototyping, or for production-
ready software development.
INDENTATION
Indentation refers to the spaces at the beginning of a code line.
Python uses indentation to indicate a block of code.
SAMPLE CODE:
INDENTATION
Python will give you an error if you skip the indentation:
SAMPLE CODE:
INDENTATION
The number of spaces is up to you as a programmer, the most
common use is four, but it has to be at least one.
SAMPLE CODE:
INDENTATION
You have to use the same number of spaces in the same block of
code, otherwise Python will give you an error:
SAMPLE CODE:
comments
Comments can be used to explain Python code.
It can be used to make the code more readable.
It can be used to prevent execution when testing code.
comments
Comments starts with a #, and Python will ignore them:
SAMPLE CODE:
comments
Comments can be placed at the end of a line, and Python will ignore
the rest of the line:
SAMPLE CODE:
comments
A comment does not have to be text that explains the code, it can
also be used to prevent Python from executing code:
SAMPLE CODE:
comments
Python does not really have a syntax for multiline comments.
To add a multiline comment you could insert a # for each line:
SAMPLE CODE:
comments
NOTE: You can use a multiline string.
Since Python will ignore string literals that are not assigned to a
variable, you can add a multiline string (triple quotes) in your code,
and place your comment inside it.
comments
SAMPLE CODE:
comments
As long as the string is not assigned to a variable, Python will read
the code, but then ignore it, and you have made a multiline
comment.
VARIABLES
Variables are containers for storing data values.
A variable is created the moment you first assign a value to it.
SAMPLE CODE:
VARIABLES
Variables do not need to be declared with any particular type, and
can even change type after they have been set.
SAMPLE CODE:
VARIABLE NAMES
A variable can have a short name (like x and y) or a more descriptive
name (age, carname, total_volume). Rules for Python variables:
A variable name must start with a letter or the underscore
character
A variable name cannot start with a number
VARIABLE NAMES
A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
Variable names are case-sensitive (age, Age and AGE are three
different variables)
A variable name cannot be any of the Python keywords.
VARIABLE NAMES
Variable names are case-sensitive.
SAMPLE CODE:
This will create two variables.
VARIABLE NAMES
Legal variable names: Illegal variable names:
VARIABLE NAMES
Multi Words Variable Names
Variable names with more than one word can be difficult to read.
There are several techniques you can use to make them more
readable:
VARIABLE NAMES
CAMEL CASE
Each word, except the first, starts with a capital letter:
VARIABLE NAMES
PASCAL CASE
Each word starts with a capital letter:
VARIABLE NAMES
SNAKE CASE
Each word is separated by an underscore character.
ASSIGN MULTIPLE VALUES
Many Values to Multiple Variables
Python allows you to assign values to multiple variables in one line:
ASSIGN MULTIPLE VALUES
SAMPLE CODE:
NOTE: Make sure the number of variables matches the
number of values, or else you will get an error.
ASSIGN MULTIPLE VALUES
One Value to Multiple Variables
And you can assign the same value to multiple variables in one line:
ASSIGN MULTIPLE VALUES
SAMPLE CODE:
ASSIGN MULTIPLE VALUES
Unpack a Collection
If you have a collection of values in a list, tuple etc. Python allows
you to extract the values into variables. This is called unpacking.
ASSIGN MULTIPLE VALUES
SAMPLE CODE:
OUTPUT VARIABLES
The Python print() function is often used to output variables.
SAMPLE CODE:
OUTPUT VARIABLES
In the print() function, you output multiple variables, separated
by a comma:
SAMPLE CODE:
OUTPUT VARIABLES
You can also use the + operator to output multiple variables:
SAMPLE CODE:
OUTPUT VARIABLES
Notice the space character after "Python " and "is ", without
them the result would be "Pythonisawesome".
OUTPUT VARIABLES
For numbers, the + character works as a mathematical operator:
SAMPLE CODE:
OUTPUT VARIABLES
In the print() function, when you try to combine a string and a
number with the + operator, Python will give you an error:
SAMPLE CODE:
OUTPUT VARIABLES
The best way to output multiple variables in the print() function is
to separate them with commas, which even support different
data types.
THANK YOU!