CHAPTER ONE Introduction to Python 1

Introduction to Python
1.0 Introduction
What is python? Python is a programming language that can be used to create web applications, mobile applications, desktop applications and pretty much everything that other programming languages do. In case you are wondering what a programming language is, a programming language is a computer language designed to communicate instructions to a machine, particularly a computer.
1.1 Brief History of python
Python was developed by Guido Van Rossum in December 1989 while he was looking for a hobby programming project that would keep him occupied during the week around Christmas. Today, python is an open source programming language which means anyone can contribute to its development.
1.2 Uses of Python
Python can be used to develop the following:
1) Desktop application
2) Mobile applications
3) Web applications
4) Game development
1.3 Advantages of Python
1) It is easy to learn.
2) It is readable because of its structure.
3) It is requires less code than other programming languages.
1.4 Installing Python
The python integrated development environment (IDLE) needs to be installed on your computer, before you can run python codes. There are various versions of python but for this book we will be looking at python 3.
To install python visit the python website and download the latest copy of python 3 IDLE

After downloading the file, install like any other software.

2.0 Working with python IDLE

Once python is installed on your computer you can launch the IDLE from Start Button>>All Programs >> Python3.4>>IDLE (Python 3.4 GUI)
NOTE: While other programming language use curly brackets ({}) to represent hierarchy python uses tab(indentation) to represent hierarchy. Eg
intfnum = 6;

fnum = 6
if(fnum> 9):

2.1 Variables
Variables are reserved memory locations to store values. You can store integers, decimals or any standard data type accepted in python with variables.
The equal to (=) is used to assign values to variables.
Suite 1

The operand on the left of the equal to (=) operator is the name of the variable and the operand to the right of the equal to operator is the value stored in the variable.
Suite 2

Note! Variables are case sensitive i.e address is different from Address. Strings as enclosed in quotes while numbers are not.

2.2 Standard data types in python
• Numbers
• Strings
• List
• Tuple
• Dictionary
The main difference between lists and turples are that lists are enclosed in square brackets () and their elements and size can charge, while turples are enclosed in parentheses ( ) and cannot be updated.
2.3 LIST
A list is a data structure in python.
List of friends
1) Jason
2) Blessing
3) Timothy
Suite 4

Each item in the lists above is separated by a comma; the square bracket encloses the list which is assigned to the variable friends.

Methods and functions are sets of prewritten code which executes an action every time it is called upon. The difference is the way with which we reference them.
Suite 5

Note! # is used to place a single line comment in python and triple quotes(“””) for multiple line comments.
Some of python’s built in functions (BIF) are outlined below:
1. len(): This is a BIF used to count the number of items in a list
Suite 6

2. append() : This can be used to add a single data at the end of a list
Suite 7

3. pop() : Is used to delete the data at the end of a list.
Suite 8

4. extend(): is used to add a collection of data to a list, unlike the append method, extend can be used to add more than one data.
Suite 9

5. remove() : this method is used to remove data from anywhere in the list.
Suite 10

6. insert () : used to insert data in a particular position in a list.
Suite 11

The above code inserts Salinas as the second item on the list.
Note! The numbering of items in a list begins at 0 which means that 2 is actually the 3rd item.
Write a program that stores the data below;
Sarah 30, Joan 20, Martina 40, Wisdom 35.
a) Remove the last student data using the appropiate function
Add the following student data; Henry 25, James 15, Tina 30.

Indicate whether the following is a method or a function.
print() Function

4.0 Loops
A loop is used when you want to perform some repeated events. There two kinds of loop in python; the while and the for loop.

Suite 13

The example above shows how to use a while loop to display contents of a list, it uses the len() function to count the number of items in the list james. As long as the variable i is less than the number of items in the list, i.e i