Introduction, Data Types, Data Scope,
Introduction, Data Types, Data Scope,
• Multi line comment- using triple double quotes (“””) or triple single
quotes (‘’’)
“”” This is a program to find net salary of an employee based on the
basic salary, provident fund, house rent allowance, dearness allowance
and income tax”””
How Python sees Variable
• In Python, a variable is seen as a tag (or name) tied to some value. For
example, the statement:
a=1
Means the value ‘1’ is created first in memory and then a tag by the name ‘a’ is
created.
Python considers the value (i.e. 1 or 2 etc..) as ‘objects’. If we change the value
of ‘a’ to new value as
a=2
Then the tag is simply changes to new value (or object). The value ‘1’now
becomes unreferenced object, it is removed by garbage collector.
Data types
Integer
• Integers are whole numbers. They have no fractional parts. Integers
can be positive or negative.
• There are two types of integers in Python:
• i) Integers(Signed) : It is the normal integer representation of whole numbers
using the digits 0 to 9. Python provides single int data type to store any
integer whether big or small. It is signed representation i.e. it can be positive
or negative.
• ii) Boolean : These represent the truth values True and False. It is a subtype of
integers and Boolean values True and False corresponds to values 1 and 0
respectively
Integer
• #Demonstration of Integer-Addition of two integer number
a=int(input("Enter the value of a:"))
b=int(input("Enter the value of b:"))
sum=a+b
print("The sum of two integers=",sum)
Output:
Enter the value of a: 45
Enter the value of b: 67
The sum of two integers= 112
Floating Point Numbers
• A number having fractional part is a floating point number.
• It has a decimal point.
• It is written in two forms :
• i) Fractional Form : Normal decimal notation e.g. 675.456
• ii) Exponent Notation: It has mantissa and exponent. e.g. 6.75456E2
Floating Point Numbers
• #Demonstration of Float Number- Calculate Simple Interest
princ=float(input("Enter the Principal Amount:")) rate=float(input("Enter the Rate of
interest:"))
time=float(input("Enter the Time period:"))
si=(princ*rate*time)/100
print("The Simple Interest=",si)
Output:
Enter the Principal Amount:5000
Enter the Rate of interest:8.5
Enter the Time period:5.5
Simple Interest= 2337.5
Complex Number
• Python represents complex numbers in the form a+bj.
#Demonstration of Complex Number- Sum of two Complex Numbers
a=7+8j
b=3.1+6j
c=a+b
print("Sum of two Complex Numbers")
print(a,"+",b,"=",c)
Output: (7+8j) + (3.1+6j) = (10.1+14j)
Strings
• A String is a group of valid characters enclosed in Single or Double
quotation marks.
• A string can group any type of known characters i.e. letters ,numbers
and special characters.
• A Python string is a sequence of characters and each character can be
accessed by its index either by forward indexing or by backward
indexing.
Strings
• #Demonstration of String- To input string & print it
my_name=input("What is your Name? :")
print("Greetings!!!")
print("Hello!",my_name)
print("How do you do?")
Output :
What is your Name? :Ananya Inkane
Greetings!!!
Hello! Ananya Inkane
How do you do?
Strings
• #Demonstration of String- To input string & print it
String1="Pankti"
print(String1) Output:
print(String1[0]) Pankti
P
print(String1[-1])
I
print(String1[1]) a
print(String1[-2]) t
Check type of the variable
• type() – built in function used to check the data type of the variable.
a=10
b=4.5
c="Hello"
d=7+8j
type(a)
int
type(b)
Float
type(c)
str
type(d)
complex
Arithmetic Operator
x=15
Relational Operator y=4
print("x>y is ",x>y)
print("x<y is ",x<y)
print("x==y is ",x==y)
print("x!=y is ",x!=y)
print("x>=y is ",x>=y)
print("x<=y is ",x<=y)
Output:
x>y is True
x<y is False
x==y is False
x!=y is True
x>=y is True
x<=y is False
v=x>y
print(v)
Output:
True
Print() – in built function to display
the data
a,b,c=2,3,4
str="Hello"
print(a)
print(a,b)
print(a,b,c)
print(a,b,c,str)
print(a,b,c,str + "Welcome!")
print(a,b,b,str + "\n Welcome!")
Input() – in built function to take
input from the user
• input() functions allow your Python Console application to read input from the user.
• Normally, the input() function reads the string from the console. Also, Python3
doesn’t distinguish whether the input is a string or a number. Whatever the user gives
as input through the console, it will receive as a String.
• Once we get string input by user in console, we can input as integer.
#read integer from user
n1 = int(input('Enter a number: '))
n2 = int(input('Enter another number: '))
print('The sum of two numbers is:', n1+n2)
ch=input("Enter a character:")
print(ch[0])