11 Lab1 Python Studs
11 Lab1 Python Studs
11 Lab1 Python Studs
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
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
Easy-to-learn
Easy-to-read
Easy-to-maintain
A broad standard library
Interactive mode
Portable
Extendable
Databases
GUI programming
Scalable
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
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
Python accepts single (), double () & triple ( or ) quotes to denote string literals, as long
as the same type of quote starts & ends the string
Example:
A hash sign (#) that is not inside a string literal begins a comment
Example 1:
Example 2:
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:
Example 1:
Example 2:
Example 3:
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:
Output:
Example:
Result:
Tuples
Tuples are enclosed in parentheses ( )
Their elements cannot be updated
Example:
Result:
Example:
Result:
Several built-in functions to perform conversion from one data type to another:
10
11
12
13
Syntax:
Example:
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:
14
Syntax:
Example:
15
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.
16