Programming
Find out more...
Write other areas where flowchart can be used.
Flowchart...
● Graphical representation of an algorithm steps.
● Defined with symbols and arrows.
● Shows steps in sequential order.
● Used in presenting flow of algorithms, or everyday
task.
Flowchart for the sum
of two numbers
Flowchart Symbols
Symbols Functions
Terminator - Beginning/End of a program.
Process (Any processing/computational function).
Decision (Decision point between two or more paths
in a flowchart).
Data (Represent any type of Input/Output data in a
flowchart).
Connector to show a jump from one point in the
process flow to another.
Flow lines (Shows the direction of flow/ Sequence of
operation)
Task
Draw a flowchart to calculate the average
of five numbers.
Write the functions of the flowchart symbols
given here.
Computer Program
Program is,
❖ A collection of instructions that are executed by
the computer to accomplish a specific result.
❖ Instructions are written in human readable
language called high level language.
❖ Instructions are translated to a computer
readable language called low level language.
Programming Languages
➔ Different ways of writing the same idea.
➔ Each programming language has its own syntax.
➔ Examples are Javascript, Java, Python, etc.
◆ Javascript: alert(“Hello”);
◆ Python: print(“Hello”)
◆ Java: System.out.print(“Hello”);
Introduction to Python Program
❖ Python is a high-level, interpreted, interactive and object-
oriented scripting language. Python is designed to be
highly readable.
❖ Created by Guido van Rossum in 1991.
❖ Save the file with “py” extension ex. Hello.py
It is used for:
● web development (server-side),
● software development,
● mathematics,
● system scripting.
Why Python?
● Runs on different platforms (Windows, Mac, Linux, etc).
● Simple syntax similar to the English language.
● Easy to write programs with fewer lines.
Input and Output
❖ Input and Output is the term referred as communication
between a computer program and the user
❖ Input is the term referred as entering data (value) to
the computer program by the user during program
execution
❖ Output is the term referred as displaying the data
(value)/information to the user by the computer
program.
List the inputs and outputs of these devices.
Output in Python
❖ print() function is often used to display the
value/information to the user by the computer.
print(“Welcome to the Python programming.”)
Simple Welcome Program
Python Online tool
Program
Program -
Output -
Variable
● The identifier (name) given to a memory location used to
store the data (value).
● The data can be changed/assigned during program
execution.
● Use variables to set the value which can be using in the
program.
● Example : To display name using variable. In this example
‘name’ is variable.
name = “Geetha”
print(name)
print(“name = “,name)
Program
Output
Program
Output
Variable
Rules To Name Variable
● Use descriptive names and not use reserved words.
● Only alpha-numeric characters and underscores (A-z, 0-9,
and _ ) are allowed.
● Must start with a letter or the underscore character.
Ex: age, age_12, _total
● Must not start with numbers. Ex: 1num, 1_num.
● Name should not have special characters.
Ex: average@123, books@, @food_items
● Variable names are case-sensitive (age, Age and AGE are
three different variables).
Identify the correct variable name convention in the
following:
● score_total = 56
● @Total = 67
● T = 34
● _age = 12
● age_1 = 23
● print
Answer
● score_total = 56 - Correct
● @Total = 67 - Incorrect (starts with special
characters)
● T = 34 - Incorrect (not descriptive name)
● _age = 12 - Correct
● age_1 = 23 - Correct
● print - Incorrect (Reserved word)
Assign value to variables
Python allows you to assign different values to multiple
variables in one line.
x= "Orange"
y = "Banana"
z = "Cherry"
print(x)
print(y)
print(z]
Assign value to variables
Python allows you to assign different values to multiple
variables in one line.
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
Assign value to variables
You can assign the same value to multiple variables
in one line.
x = y = z = "Orange"
print(x)
print(y)
print(z)
Input Function - Syntax
In python, input function is used to give the data from
the user to the computer program during program
execution.
Syntax : input()
Python Comments
❖ Comments are text notes added to the program to provide
explanatory information about the source code
❖ Instructions or Information mentioned within the
comments are considered as “Non-executable” statements
(Not executed by the Python compiler)
❖ Single Line (#) and Multi Line comments (‘’’ ‘’’ – enclosed
within three quotes)
❖ Try out this code and check the output
➢ Single line Comment
#print("Hello, World!")
print("Cheers, Mate!")
Python Data Types
● Data Type is defined as the type (format) of the data in the
program.
Data Type Data type in Description
Name Python
Integer int Whole numbers either positive or
negative eg: 134
Real (or) Float float Positive or negative decimal values ex:
34.56
Character str Single character or symbol (ex: ‘A’,’$’,’6’)
String str More than one character (ex: “Geetha”,
“3.455”, ‘
[email protected]’)
Boolean bool Boolean value True (or) False
Python Operators
Arithmetic Operators
● Performs numerical computational tasks.
Addition (+) result = num1 + Add the value in the variables num1,
num2 num2 and store it in the result variable.
Subtraction result = num1 - Subtract the value in the variables
(-) num2 num1, num2 and store it in the result
variable.
Multiplication result = num1 * Multiply the value in the variables
(*) num2 num1, num2 and store it in the result
variable.
Division (/) result = num1 / Divide the value in the variables num1,
num2 num2 and store it in the result variable.
Integer (or) result = num1 // Gives quotient value of the division and
Floor Division num2 discards the remainder.
(//)
Modulus (%) Result = Returns only the remainder of the
Program : sum of two numbers
Output
Operator Precedence
Arithmetic Operators
-1
Write a simple program to find the addition,
subtraction, multiplication, division, modulus and
integer division of the two numbers.
Reserved words in Python
Reserved words are case-sensitive in lowercase except None,
False, True and must be used exactly as defined.
False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass print
break except in raise
Type function
This function returns data types of the variable
which holds string, integer, float values etc.
Task - 1
● Try the following program in Python editor to print the data
type of the variable.
Task - 2
Write a simple program to check the data type of
the integer, real, boolean and character
variables.
Program to check the data type of the integer, real, string variable
values
Data Type Casting
The process of converting from one data type into another
data type.
By default, input function gives user entered value as String
data types.
Type cast functions are,
int( ) - Converts from other data type to integer data type.
float( ) - Converts from other data type to float data type.
str( ) - Converts from other data type to string data type.
Output of Program 1
Output of Program 2
What is the output of these programs?
Program 1
Program 2
Assignment and Arithmetic Operators
Operators Example Description
= c=a+b Assigns value of a+b into c.
+= c+=a Equivalent to c=c+a
-= c-=a Equivalent to c=c-a
*= c*=a Equivalent to c=c*a
/= c/=a Equivalent to c=c/a
//= c//=a Equivalent to c=c//a
Example - Assignment and Arithmetic
Operator
Output
Examples - Comparison Operators
Comparison Operators
== a==b Returns true if a and b are equal, otherwise
false.
!= a!=b Returns true if a and b not equal, otherwise
false.
< a<b Returns true if a is less than b, otherwise false.
<= a<=b Returns true if a is less than equal to b,
otherwise false.
> a>b Returns true if a is greater than b, otherwise
false.
>= a>=b Returns true if a is greater than equal to b,
otherwise false.
Simple - IF Statement Syntax
• IF statement is called as conditional or selection statement.
• Condition is nothing but comparison between two values (or)
between a variable and a value (or) between variables using
comparison operator
• Condition always return boolean value True (or) False.
• If condition is true (satisfied), then it executes statements
defined within the IF block.
• In Python, Indentation is used to define the block of codes to
be executed when the condition is true.
Example for Simple-IF
Output
If-Else Statement Syntax
• If condition is true (satisfied), then it executes
statement defined within the IF block. Otherwise,
it executes statements defined within ELSE block.
• In Python, Indentation is used to define the
block of codes to be executed when the condition
is true/false.
Example IF-Else
Output
Python Program - The Largest of three numbers
using nested if-else
Logical Operators
Operators Example Description
and X and Y Returns true if X and Y are true
Returns false if either x or y is false.
or X or Y Returns true if either of X or Y is true
Returns false otherwise.
not not X Returns reverse value of X.
Returns false if X is true
Returns true if X is false
Table - AND Logical Operators
Condition 1 Condition 2 Output
(Ex: a<b) (Ex: c<d) Ex: if(Condition 1 and
Condition 2)
True True True
True False False
False True False
False False False
Example - Logical AND Operator
Table - OR Logical Operators
Condition 1 Condition 2 Output
(Ex: a<b) (Ex: c<d) Ex: if(Condition 1 or
Condition 2)
True True True
True False True
False True True
False False False
Example - Logical OR Operator
Output
Table - NOT Logical Operators
Condition 1 Output
(Ex: a<b) Ex: if(not Condition 1)
True False
False True
Example - NOT Logical Operators
Output
Program to display direction name using if-elif-else
Program for Arithmetic Calculator
Answer
Homework- Selection Statement
1. Write Python program:
● if the choice is “G”, display “GO”
● if the choice is “Y”, display “READY”
● if the choice is “R”, display “STOP”
● otherwise, display “Invalid Choice”
1. Write Python program to find the given number is odd
or even.
Answer
Program 2
● Prepare a Python program to evaluate the given marks.
Program 1
● Write a Python program to check whether the
entered year is a Leap year or not.
○ Year is exactly divisible by four, except for years
that are exactly divisible by 100.
○ Centurial years are leap years if they are
exactly divisible by 400, then print leap year.
○ Otherwise, print not a leap year.
Program 3
● An automatic window system is operated by
software program to open or close the window.
Window should open when any of the following
condition satisfied:
○ Temperature is too high (> 100 degree
Fahrenheit).
○ Rain is not detected.
Formative Assessment 2 Questions.
● Write a Python program to convert Kilometer to Meter.
● Write a Python program to convert Celsius to Fahrenheit. Formula is
Fahrenheit = Celsius * 1.8 + 32
● Write a Python program to calculate area of a triangle. Formula is
Area= ½ * base * height
● Write a Python program to calculate the Force required to move
objects by any acceleration and mass value.