Module 2 Integrative Programming 2
Module 2 Integrative Programming 2
Tuguegarao City
Course Code :
Course Title : INTEGRATIVE PROGRAMMING 2
MODULE No. 02
TITLE: INTRO TO PYTHON PROGRAMMING
INTRODUCTION Python is a general-purpose interpreted, interactive, object-oriented, and
high-level programming language. It was created by Guido van Rossum
during 1985- 1990. Like Perl, Python source code is also available under
the GNU General Public License (GPL). This tutorial gives enough
understanding on Python programming language.
LEARNING 1.
OUTCOMES
LEARNING 1.
OBJECTIVES
In an interpreted language, rather than doing all the translation at once, the compiler translates some of
the code written (maybe a line or two) in a human readable language to an intermediate form, and then
this form gets "interpreted" to 0s and 1s that the machine understands and immediately executes.
Thus, the translation and execution are going on simultaneously.
Python is an interpreted programming language. One standard environment in which students often
write python programs is IDLE (Integrated Distributed Learning Environment). This environment
offers students two separate ways to write and run python programs. Since the language is interpreted,
there exists an option for students to write a single line of python code and immediately see the
results. Alternatively, students can open a separate window, put all of their commands in this window
first, and then interpret their program.
Page 1
UNIVERSITY OF CAGAYAN VALLEY
Tuguegarao City
The first method lets students see, in real time, the results of their statements. The second follows the
more traditional model of composing an entire program first before compiling and seeing the results.
For learning purposes, the first technique is very useful. Ultimately however, students must develop
their programs utilizing the second method.
What is Python?
Python is a popular programming language. It was created by Guido van Rossum, and released in
1991.
It is used for:
Why Python?
Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
Python has a simple syntax similar to the English language.
Python has syntax that allows developers to write programs with fewer lines than some other
programming languages.
Python runs on an interpreter system, meaning that code can be executed as soon as it is
written. This means that prototyping can be very quick.
Python can be treated in a procedural way, an object-oriented way or a functional way.
Good to know
The most recent major version of Python is Python 3.9.1, which we shall be using in this
tutorial. However, Python 2, although not being updated with anything other than security
updates, is still quite popular. (W3schools.com)
In this tutorial Python will be written in a text editor. It is possible to write Python in an
Integrated Development Environment, such as Thonny, Pycharm, Netbeans or Eclipse which
are particularly useful when managing larger collections of Python files.
Python was designed for readability, and has some similarities to the English language with
influence from mathematics.
Python uses new lines to complete a command, as opposed to other programming languages
which often use semicolons or parentheses.
Python relies on indentation, using whitespace, to define scope; such as the scope of loops,
functions and classes. Other programming languages often use curly-brackets for this purpose.
Page 2
UNIVERSITY OF CAGAYAN VALLEY
Tuguegarao City
Example
print("Hello, World!") Hello World!
Page 3
UNIVERSITY OF CAGAYAN VALLEY
Tuguegarao City
Python
is
fun!
One idea might be to physically hit the enter key after typing in "Python" in the middle of the print
statement. Unfortunately, doing this yields the error:
EOL stands for "end of line." The meaning of the error is that the interpreter was waiting to read in the
end of the string literal, denoted by the second double quote, before the end of the line, since all
python statements must fit on a single line. When the interpreter encountered the end of the line,
which meant the end of the statement as well, it realized that the string literal had not been finished.
In order to "fix" this issue, we need some way to denote to the interpreter that we wish to advance to
the next line, without having to literally type in the enter key. python, as many other languages do,
provides escape sequences to deal with issues like this one. An escape sequence is a code for a
character not to be taken literally.
For example, the escape sequence for the new line character is \n. When these two characters are
encountered in sequence in a string literal, the interpreter knows not to print out a backslash followed
by an n. Rather, it knows that these two characters put together are the code for a new line character.
Thus, to print out
Python
is
fun!
to the screen with a single print,
we can do the following: print("Python\nis\nfun!")
Page 4
UNIVERSITY OF CAGAYAN VALLEY
Tuguegarao City
Or by creating a python file on the server, using the .py file extension, and running it in the Command Line:
Python Indentation
Where in other programming languages the indentation in code is for readability only, the indentation in Python
is very important.
Example
if 5 > 2:
print("Five is greater than two!")
Example
Syntax Error:
if 5 > 2:
print("Five is greater than two!")
Example
if 5 > 2:
print("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!")
You have to use the same number of spaces in the same block of code, otherwise Python will give you an error:
Page 5
UNIVERSITY OF CAGAYAN VALLEY
Tuguegarao City
Example
Syntax Error:
if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")
Python Variables
Example
Variables in Python:
x = 5
y = "Hello, World!"
randy=int(50)
Comments
Comments start with a #, and Python will render the rest of the line as a comment:
Example
Comments in Python:
#This is a comment.
print("Hello, World!")
Page 6