11 lab1 python studs

16
CSC305 PROGRAMMING PARADIGMS PYTHON: LAB 1 PREPARED BY: NYCJ@FSKM, UiTM (PERLIS) 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 beginner’s 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 1980’s & early 1990’s 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

Upload: zul-zahid

Post on 16-Jan-2016

60 views

Category:

Documents


3 download

DESCRIPTION

yeah ohhsfkklsf

TRANSCRIPT

Page 1: 11 Lab1 Python Studs

CSC305 – PROGRAMMING PARADIGMS PYTHON: LAB 1

PREPARED BY: NYCJ@FSKM, UiTM (PERLIS) 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 beginner’s 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 1980’s & early 1990’s 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

Page 2: 11 Lab1 Python Studs

CSC305 – PROGRAMMING PARADIGMS PYTHON: LAB 1

PREPARED BY: NYCJ@FSKM, UiTM (PERLIS) 2

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

Page 3: 11 Lab1 Python Studs

CSC305 – PROGRAMMING PARADIGMS PYTHON: LAB 1

PREPARED BY: NYCJ@FSKM, UiTM (PERLIS) 3

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:

Page 4: 11 Lab1 Python Studs

CSC305 – PROGRAMMING PARADIGMS PYTHON: LAB 1

PREPARED BY: NYCJ@FSKM, UiTM (PERLIS) 4

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:

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:

“\n” is used to create new line

Page 5: 11 Lab1 Python Studs

CSC305 – PROGRAMMING PARADIGMS PYTHON: LAB 1

PREPARED BY: NYCJ@FSKM, UiTM (PERLIS) 5

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

Page 6: 11 Lab1 Python Studs

CSC305 – PROGRAMMING PARADIGMS PYTHON: LAB 1

PREPARED BY: NYCJ@FSKM, UiTM (PERLIS) 6

3.4 Python strings: Example:

Output:

3.5 Python list Lists are the most versatile of Python’s 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 Python’s 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 Tuples Lists are enclosed in brackets ([ ]) Tuples are enclosed in parentheses ( ) Their elements & size can be changed Their elements cannot be updated

Page 7: 11 Lab1 Python Studs

CSC305 – PROGRAMMING PARADIGMS PYTHON: LAB 1

PREPARED BY: NYCJ@FSKM, UiTM (PERLIS) 7

Example:

Result:

3.7 Python’s dictionary: Python’s 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:

Page 8: 11 Lab1 Python Studs

CSC305 – PROGRAMMING PARADIGMS PYTHON: LAB 1

PREPARED BY: NYCJ@FSKM, UiTM (PERLIS) 8

3.8 Data type conversion Several built-in functions to perform conversion from one data type to another:

Page 9: 11 Lab1 Python Studs

CSC305 – PROGRAMMING PARADIGMS PYTHON: LAB 1

PREPARED BY: NYCJ@FSKM, UiTM (PERLIS) 9

4. PYTHON BASIC OPERATORS

4.1 Arithmetic operators:

Page 10: 11 Lab1 Python Studs

CSC305 – PROGRAMMING PARADIGMS PYTHON: LAB 1

PREPARED BY: NYCJ@FSKM, UiTM (PERLIS) 10

4.2 Comparison operators:

Page 11: 11 Lab1 Python Studs

CSC305 – PROGRAMMING PARADIGMS PYTHON: LAB 1

PREPARED BY: NYCJ@FSKM, UiTM (PERLIS) 11

4.3 Assignment operators:

Page 12: 11 Lab1 Python Studs

CSC305 – PROGRAMMING PARADIGMS PYTHON: LAB 1

PREPARED BY: NYCJ@FSKM, UiTM (PERLIS) 12

4.4 Bitwise operator

4.5 Logical operators:

Page 13: 11 Lab1 Python Studs

CSC305 – PROGRAMMING PARADIGMS PYTHON: LAB 1

PREPARED BY: NYCJ@FSKM, UiTM (PERLIS) 13

4.6 Membership operators:

4.7 Identity operators:

5. PYTHON DECISION MAKING 5.1 If statement’s example:

Page 14: 11 Lab1 Python Studs

CSC305 – PROGRAMMING PARADIGMS PYTHON: LAB 1

PREPARED BY: NYCJ@FSKM, UiTM (PERLIS) 14

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:

Page 15: 11 Lab1 Python Studs

CSC305 – PROGRAMMING PARADIGMS PYTHON: LAB 1

PREPARED BY: NYCJ@FSKM, UiTM (PERLIS) 15

5.4 Nested if statements Syntax:

Example:

Page 16: 11 Lab1 Python Studs

CSC305 – PROGRAMMING PARADIGMS PYTHON: LAB 1

PREPARED BY: NYCJ@FSKM, UiTM (PERLIS) 16

===== 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.