G7 Programming
G7 Programming
Flowchart...
◆ 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.
It is used for:
Output -
Variable
● The identifier (name) given to a memory location used to store the data
(value).
● Use variables to set the value which can be using in the program.
name = “Geetha”
print(name)
print(“name = “,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.
Real (or) Float float Positive or negative decimal values ex: 34.56
Subtraction (-) result = num1 - num2 Subtract the value in the variables num1,
num2 and store it in the result variable.
Multiplication result = num1 * num2 Multiply the value in the variables num1,
(*) num2 and store it in the result variable.
Division (/) result = num1 / num2 Divide the value in the variables num1, num2
and store it in the result variable.
Integer (or) result = num1 // num2 Gives quotient value of the division and
Floor Division discards the remainder.
(//)
Modulus (%) Result = num1%num2 Returns only the remainder of the division of
the two numbers.
Program : sum of two numbers
Output
Operator Precedence
Arithmetic Operators - 1
as elif if or yield
● Try the following program in Python editor to print the data type of the
variable.
Task - 2
Program 1
Program 2
Output of Program 1
Output of Program 2
Assignment and Arithmetic Operators
Operators Example Description
Output
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
If-Elif-Else Statement Syntax
• If condition is true (satisfied), then it executes statement
defined within the IF block. Otherwise, it checks the
condition of the followed elif and if it is true it executes
statements defined within, otherwise, ELSE block is
executed.
• We can define as many as elif conditions as per the
requirement. If none of the condition is satisfied, else block
will be executed.
Program to check whether the entered number is a
negative or positive or zero using if-elif-else conditional
structure.