Chapter 1 Introduction to Python
Chapter 1 Introduction to Python
Chapter 1 Introduction to Python
Learning Outcome
Understand the meaning of program and programming
Meaning of terms like syntax, Interpreter, execution of program
Learn to code in IDLE screen
Programming Languages: The language used to specify this set of instructions to the
computer is called a programming language.
Python is regarded as the simple and easy to learn programming language because of the
following reasons.
Easy to learn and code.
It has a simple syntax and readability.
Python code is 3 to 4 times shorter than Java and 8 to 10 times shorter than C++.
It can run on various operating systems and hardware platforms.
Python is used for testing microchips, building video games, web applications and a lot
more.
Python is also helpful in web development. Many popular web services and applications
are built using Python.
It has been used by organizations such as NASA, YouTube and Instagram. It is also used
for writing Google Apps.
Language translators like compilers and interpreters are needed to translate the source code
into machine language.
Python uses an interpreter to convert its instructions into machine language, so that it can be
understood by the computer.
An interpreter processes the program statements line by line.
Fig 1
IDLE is a text editor which helps you get your code (program) right. It is also known as the
Python Interactive Mode.
The code that you want to execute is typed after the entry prompt (>>>).
Fig 3
As you can see, this was a pretty easy task. This is the beauty of the Python programming
language.
A new blank window appears to type codes and save it for later use
Fig 4
Fig 5
The program in script mode will not run without saving the program.
Saving the program
To Save the program click the File menu and click the Save As option.
Type the file name in the Save As dialog box.
The file is saved with .py as a file extension.
Running a python program
To run the program, click the Run menu and then click the Run Module.
The output of the program appears on shell. (Fig 6)
Fig 6
When the computer executes the program and finds a mistake(s), it shows an error message.
Such mistake in the instruction or code is known as syntax error.
Fig 7
********************
Chapter 2 Data types Operators in Python:
Math in Python
You can use python as a calculator. Following are a few examples. You will get output
in blue colour.
Operators in Python
Operator Name Example Answer
+ Add 5+5 10
- Substract 5-5 0
* Multiply 5*5 25
/ Divide 5/2 5//2 2.5 2
% Modulus (Remainder) 20%8 4
** Exponent 2**5 32
Practical Activity:
Python Statements:
1. print(“3+2 is an addition”)
2. print(“Addition=”,3+2)
3. print(“21 divided by 3=”,21/3)
4. print(“18 divided by 5=”, 18/5, “remainder=” , 18%5)
5. print(“18 divided by 5=”, 18//5, “remainder=” , 18%5)
********************
Chapter 3 Inputs in Python
Python language provides numerous in-built functions that can be used at the Python
prompt. Functions like input( ) and print( ) are widely used for input and output
operations respectively. You have learnt the print ( ) function, now let’s learn how to
use the input () function.
input () gives flexibility to take input from the user.
Syntax:
input ( [ prompt ])
where ‘prompt’ is the string you wish to display on the screen. It is optional.
Practical Activity:
To convert strings such as ‘33’’ into a number use int ( ) or float ( ) functions.
Practical Activity :
Write a program to add two numbers provided by the user.
Same program can be written in other way also , check the following:
Output:
********************
Chapter 4 Conditional Statements in Python
Like other programming languages,conditional statements check for the condition and
accordingly evaluate the result.
Python assumes any non zero and non null values as TRUE, and if it is either zero or
null,then it is assumed as FALSE value.
If Statement
The if statement contains a logical expression. By using this expression ,the data is
compared and a decision is made based on the result of the comparisons.
Syntax:
if condition :
statement(s)
The indentation implies that its execution is dependent on the condition. There
is no limit on the number of statements that can appear as a block under the if
statement.
Practical Activity:
Write a program to check if the number is positive and print the message. Ask the user
for input a number.
If …..else statement
An else statement can be combined with if statement that will execute if the condition
resolves FALSE value.
Syntax:
if condition:
statement(s)
else:
statement(s)
Practical Activity:
If…elif…else Statement:
The elif statement allows you to check multiple conditions for True. It executes a block
of code as soon as one of the conditions evaluates to true.
Syntax:
if condition:
statement(s)
elif condition:
statement(s)
elif condition:
statement(s)
else:
statement(s)
Practical Activity:
Write a program to check if the number is zero, positive or negative. Print appropriate
message. And ask the user to input a number.
# Program to check if number is positive or negative or zero.
num = float(input("Enter a number:"))
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative Number")
Output:
********************
Chapter 5 Logical Operators and loops
********************
Chapter6 Basics of for Loop
for loop : The for loop is used when you are sure about how many times a loop body
will be executed. It is known as definite loop.
Syntax
for<variable> in range(initial value,final value,step value):
Examples
for i in range(1,5)
output
1
2
3
4
Remember the value displayed would be from 1 to 4
for i in range(1,10,2)
output
1
3
5
7
9
Programs
1. Write a program to display first 10 natural number.
2. Write a program to find sum of odd numbers between 10 and 30
3. Write a program to display table of input number.
4. Write a program to input start value, stop value and step value and display the
output (Assuming start<stop)
********************