G7 Programming
G7 Programming
◆ 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).
Output -
Variable
● The identifier (name) given to a memory location used to
store the data (value).
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.
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
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.
(//)
Output
Operator Precedence
Arithmetic Operators
-1
as elif if or yield
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.
Output
Examples - Comparison Operators
Comparison Operators
== a==b Returns true if a and b are equal, otherwise
false.
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
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”