INTRODUCTION TO PYTHON
CHAPTER 2
PERFORMING OUTPUT
• The keyword print is used for output
PYTHON SYNTAX
• End of commands are marked by a new line
• Indentation in python is very important – The second example generates an
error
COMMENTS
• The hash symbol is used for a single line
• Three quotation marks for multiple lines
VARIABLES IN PYTHON
• We don’t need to declare them. Simply start by assigning.
• They get their data type depending on how they are assigned
• Python is Case Sensitive
• Output can be combined using a +
DATA TYPES
• Python decides on the data type upon assignment. Data
items enclosed in quotes are assumed to be string. Python
has no character data type.
• Other Data types are numeric data types such as Int,
Float, and Complex.
• The keyword type is used to print the data type.
PYTHON COLLECTIONS
• Collections in Python are similar to arrays i.e. store several variables.
• There are four types of collections:
• List - is changeable
• Tuple - unchangeable
• Set - unindexed and unchanged.
• Dictionary – indexed and changeable.
LISTS
• List - changeable
• They are declared using square brackets
LIST METHODS
• Python has a set of built-in methods that you can use on their collection of data structures.
• There are other functions. Try the len() function. What do you think it does?
TUPLES
• Tuple - unchangeable
• They are declared using round brackets
TURPLE METHODS
Try the following turple methods
• cmp(tuple1, tuple2) - Compares elements of both tuples.
• len(tuple) - Gives the total length of the tuple.
• max(tuple) - Returns item from the tuple with max value.
• min(tuple) - Returns item from the tuple with min value.
• tuple(seq) - Converts a list into tuple.
SETS
• Set - unindexed and unchanged.
• They are declared using curly brackets
• They also have their inbuilt methods
DICTIONARIES
Dictionary – indexed and changeable.
Dictionaries are written with curly brackets, and they have keys and values.
They also have their inbuilt methods
CASTING IN PYTHON
• This is done to convert data from one type to another
• Simply enclose the variable with the data to convert to e.g. int(), float(), str()
PERFORMING INPUT
• The keyword Input is used
• Numbers input are taken as string by default. To use them in a calculations
you must cast them as numeric
ARITHMETIC'S IN PYTHON
• Program that can request the user for two numbers and display the sum
Operator Name Example
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
• Write a program that generates the output below
CONTROL STRUCTURES- DECISION
• The conditional operators are as follows:
Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
• Note the syntax and indentation
• Write a program that tells the user if a score input is a pass. Assume a pass
marks of 60%
• Write a program that tells the user if a number input is even or odd
CONTROL STRUCTURES - LOOPS
• We repeat the previous example 3 times using a
while loop
• Notice how indentation is used to mark the
beginning and end of a control structure.
CONTROL STRUCTURES - LOOPS
• The other common loop in Python is the for
loop
• Unlike in many other languages this loop in
python is used to iterate through python
collections I.e. lists, tuples, sets, and
dictionaries.
FUNCTIONS
• In Python a function is defined using the def keyword
• Indent the body of the function
• Use full colon after function definition
• Passing of parameters and returning of values is done the
same way as in other programming languages
• Write a program that can be used to +, -, x, or ÷ depending on
the users choice. Have a separate function to display the
results.
OBJECT ORIENTED PROGRAM
• Note how the class student and object stud1 are
created.
• Note the constructor __init__() function used to assign
values to object and carry out operations needed when
the object is being created.
• The self parameter has to be the first parameter of any
function in the class, and is used to access variables
that belongs to the class. It is a reference to the current
object of the class. It does not have to be named self ,
you can call it whatever you like.
• Write an object oriented program that can be used to
get the name, quantity and cost of items bought in a
store. The program should be able to ask the user for
the data. It should have a function to get the total cost
of items bought, and a function to display these costs.
Make use of three objects.
INHERITANCE
• Add an class for an employee that
inherits from persons. An employee
should have a job description, hours
worked and pay per hour.
• Display a PASS or FAIL for
students. Assume a 60% pass mark
• Display total pay for an employee.