CSC305 PROGRAMMING PARADIGMS
PYTHON: LAB 1
1. PYTHON OVERVIEW
Python is interpreted:
o It is processed at runtime by the interpreter & need not to compile the program before
executing it
Python is interactive
Python is object-oriented
Python is beginners language:
o Python is a great language for the beginner programmers & supports the development of
a wide range of applications from simple text processing to WWW browsers to games
1.1 History of Python:
Python was developed by Guido van Rossum in the late 1980s & early 1990s at the National
Research Institute for Mathematics & Computer Science in the Netherlands.
Python is derived from many other languages, including ABC, Modula-3, C, C++, Algol68,
SmallTalk & Unix shell & other scripting languages
1.2 Python features:
Easy-to-learn
Easy-to-read
Easy-to-maintain
A broad standard library
Interactive mode
Portable
Extendable
Databases
GUI programming
Scalable
PREPARED BY: NYCJ@FSKM, UiTM (PERLIS)
CSC305 PROGRAMMING PARADIGMS
PYTHON: LAB 1
2. PHYTON BASIC SYNTAX
2.1 Python identifiers:
A Python identifier is a name used to identify a variable, function, class, module or other object
An identifier starts with a letter (A..Z, a..z) or an underscore (_) followed by zero or more
letters, underscores & digits (0..9)
Python is a case sensitive programming language
2.2 Reserved words:
2.3 Lines & indentation:
There are no braces to indicate blocks of code for class & function definitions or flow control
Blocks of code are denoted by line indentation
Example 1:
Example 2:
Will generate an error
PREPARED BY: NYCJ@FSKM, UiTM (PERLIS)
CSC305 PROGRAMMING PARADIGMS
PYTHON: LAB 1
2.4 Quotation in Python:
Python accepts single (), double () & triple ( or ) quotes to denote string literals, as long
as the same type of quote starts & ends the string
Example:
PREPARED BY: NYCJ@FSKM, UiTM (PERLIS)
CSC305 PROGRAMMING PARADIGMS
PYTHON: LAB 1
2.5 Comments in Python:
A hash sign (#) that is not inside a string literal begins a comment
Example 1:
Example 2:
2.6 Waiting for the user:
\n is used to create new line
2.7 Multiple statement groups as suites:
A group of individual statements, which make a single code block are called suites in Python
Compound or complex statements. (if, while, def & class) are those which require a header line &
a suite
Example:
3. PYTHON VARIABLE TYPES
3.1 Assigning values to variables:
The equal sign (=) is used to assign values to variables
Example 1:
PREPARED BY: NYCJ@FSKM, UiTM (PERLIS)
CSC305 PROGRAMMING PARADIGMS
PYTHON: LAB 1
Example 2:
Example 3:
3.2 Standard data type
Python has 5 standard data types:
o Numbers
o String
o List
o Tuple
o Dictionary
3.3 Python numbers:
Number objects are created when you assign a value to them. Example:
You can also delete the reference to a number object by using the del statement. Example:
Python supports 4 different numerical types:
o int
o long
o float
o complex
PREPARED BY: NYCJ@FSKM, UiTM (PERLIS)
CSC305 PROGRAMMING PARADIGMS
PYTHON: LAB 1
3.4 Python strings:
Example:
Output:
3.5 Python list
Lists are the most versatile of Pythons compound data types
A list contains items separated by commas & enclosed within square brackets ( [ ])
Items belonging to a list can be of different data type
Example:
Result:
3.6 Pythons tuple:
A tuple is another sequence data type that is similar to the list
A tuple consists of a number of values separated by commas
Unlike list, tuples are enclosed with parentheses
The main different between lists & tuples are:
Lists
Lists are enclosed in brackets ([ ])
Their elements & size can be changed
PREPARED BY: NYCJ@FSKM, UiTM (PERLIS)
Tuples
Tuples are enclosed in parentheses ( )
Their elements cannot be updated
CSC305 PROGRAMMING PARADIGMS
PYTHON: LAB 1
Example:
Result:
3.7 Pythons dictionary:
Pythons dictionaries are kind of hash table type
They consist of key-value pairs
o A dictionary key can be almost any Python type, but are usually numbers or strings
o Values can be any arbitrary Python object
Dictionaries are enclosed by curly braces ({ }) & values can be assigned & accessed using the
square braces ([ ]).
Example:
Result:
PREPARED BY: NYCJ@FSKM, UiTM (PERLIS)
CSC305 PROGRAMMING PARADIGMS
PYTHON: LAB 1
3.8 Data type conversion
Several built-in functions to perform conversion from one data type to another:
PREPARED BY: NYCJ@FSKM, UiTM (PERLIS)
CSC305 PROGRAMMING PARADIGMS
PYTHON: LAB 1
4. PYTHON BASIC OPERATORS
4.1 Arithmetic operators:
PREPARED BY: NYCJ@FSKM, UiTM (PERLIS)
CSC305 PROGRAMMING PARADIGMS
PYTHON: LAB 1
4.2 Comparison operators:
PREPARED BY: NYCJ@FSKM, UiTM (PERLIS)
10
CSC305 PROGRAMMING PARADIGMS
PYTHON: LAB 1
4.3 Assignment operators:
PREPARED BY: NYCJ@FSKM, UiTM (PERLIS)
11
CSC305 PROGRAMMING PARADIGMS
PYTHON: LAB 1
4.4 Bitwise operator
4.5 Logical operators:
PREPARED BY: NYCJ@FSKM, UiTM (PERLIS)
12
CSC305 PROGRAMMING PARADIGMS
PYTHON: LAB 1
4.6 Membership operators:
4.7 Identity operators:
5. PYTHON DECISION MAKING
5.1 If statements example:
PREPARED BY: NYCJ@FSKM, UiTM (PERLIS)
13
CSC305 PROGRAMMING PARADIGMS
PYTHON: LAB 1
5.2 If...else statement
Syntax:
Example:
5.3 The elif statements:
It allows you to check multiple expressions for truth value & execute a block of code as soon as
one of the conditions evaluates to true
Syntax:
Example:
PREPARED BY: NYCJ@FSKM, UiTM (PERLIS)
14
CSC305 PROGRAMMING PARADIGMS
PYTHON: LAB 1
5.4 Nested if statements
Syntax:
Example:
PREPARED BY: NYCJ@FSKM, UiTM (PERLIS)
15
CSC305 PROGRAMMING PARADIGMS
PYTHON: LAB 1
===== EXERCISES =====
Question 1
a) Run the given program codes:
name = raw_input("What is your name?")
print "Welcome " + name #Line 2: greeting message
b) Modify the given program codes so that the program will prompt the user to enter the month
he/she was born after the greeting message. Then, display the month.
c) Extend your program in (b) so that the program is able to display the number of days in the
month specified by the user.
Question 2
a) Instantiate a dictionary in which the keys are the months in the third quarter of the year and the
values are the number of days in the corresponding month. Display the dictionary, the keys and
the values.
b) Add the tenth month of the year to the dictionary and display the value of that month only.
PREPARED BY: NYCJ@FSKM, UiTM (PERLIS)
16