This document discusses data types and variables in Python. It explains that a variable is a name that refers to a memory location used to store values. The main data types in Python are numbers, strings, lists, tuples, and dictionaries. It provides examples of declaring and initializing different types of variables, including integers, floats, characters, and strings. Methods for assigning values, displaying values, and accepting user input are also demonstrated. The document also discusses type conversion using functions like int(), float(), and eval() when accepting user input.
2. Variable:
Variable is a name that is used to refer to memory location.
Python variable is also known as an identifier and used to hold
value.
A Python variable is a reserved memory location to store
values. In other words, a variable in a python program gives
data to the computer for processing.
How we declare variable.
ch=‘A’ or ch=“A” nm=‘Computer’ or nm=“Computer”
a=10 b=10.89
Identifier Identifier
3. Methods of declaring and initializing variables:
To assign value to a variable we need to use (= equal)
assignment operator.
Example: If we want to declare a integer variable A and want
to store 10.So we use =(assignment operator) and assign
value 10 to variable A.
A = 10
A is L-value 10 is R-valueAssignment
operator
4. Components of variable or objects:
Identity Type Value
It refers to the
memory address
which does not
change once it has
been created.
If you want to
determine the data
type of variable ,that
what type of value
does it hold.
It refers to the data
store inside the
variable that we
declared.
Example: A=10 So A is a variable and 10 is the value
store inside it.
Now how you know the memory address and datatype name
of a variable A.
5. Example: A=10
Identity To know the identity or memory address use id().
A=10
print(id(A))
----OUTPUT---
-
1437714496
Type To know the data type of a variable use type()
A=10
print(type(A))
----OUTPUT---
-
<class 'int'>
6. Different methods of assignments:
If we want to assign value 10 to A and 20 to B
A=10
B=20
If we want to assign value 10 to A ,B , and C
A=B=C=10
7. Different methods of assignments:
If we want to assign multiple value in a multiple variables : A
value 10 , B value 20 and C value 30
A=10
B=20
If we want to assign multiple value in a multiple variables using
single line
A ,B , C= 10 , 20 , 30
C=30
8. Data Type:
Standard Data Types
Data types refer to the type of data used in the
program. The data stored in memory can be of many
types.
In this we discuss :
Python has five standard data types:
Dictionary
Number
String Lis
t
Tuple
9. Number Data type
It store numerical values. The integer, float, and complex
values belong to a Python Numbers data-type.
Example:
It denotes
whole numbers
without
fractional part
It denotes
floating
values(with
fractional part)
It is made up of pairs real and
imaginary numbers. Where 2 is a real
part and 5j is imaginary(5 is float and
j is square root of an imaginary
number)
10. How to Declare , Initialize and use a Variable
Example: If we want to store a value 20 in a variable A.
Means we need to declare a variable A and store value 20 as
integer / number(0-9)
Example: If we want to store a value 78.5263 in a variable A
and -5.235 in a variable B
Means we need to declare a variable A
11. How to declare character variable?
Example: If we want to store a value ‘A’ character in a variable
ch. Means we need to declare a variable ch and store
character ‘A’ in it.
Character , words or paragraph use single quotes
(‘) or double quotes(“)
12. How to Declare , Initialize and use a Variable
Example: If we want to store a value ‘computer’ word or
string in a variable nm. Means we need to declare a variable
nm and store word ‘computer’ in it.
word=‘computer’
Character , words, string or paragraph use single quotes (‘)
or double quotes(“)
word=“computer”OR
How to store string value:
line=‘Welcome to python’ line=“Welcome to python”OR
13. How to Display the values inside the Variable?
To join statement with variable use
,(comma) sign
14. How to Display the values inside the Variable?
Method 1: to initialize variable Method 2: to initialize A,B in a
single line
20. Now if we want to store integer,floating, character, and string value in a
variable
Method 1: to initialize variable
21. Method 2: to initialize variable with mixed values in a single line
22. If we want that the value of the variable enter through keyboard
at runtime.
In Python, we have the input() function to accept values from the
user at runtime. By default value accept using input is string.
For Example if we want to accept the name of a person and display it.
nm=input(“Enter the name:”)
print(“Name:”,nm)
-----Output----
Enter the name: Max
Name: Max
23. Example: If we want to accept number using input() function.
no=input(“Enter the value:”)
print(“No=“,no)
-----Output----
Enter the value: 20
No=‘20’
So how we convert the string into integer or float.
Means how we accept the floating or integer value from user at
runtime.
We need to convert the string into float or integer using type
conversation functions provided by python
24. int() float() eval()
It convert
the string
value into
integer
value only.
It convert
the string
value into
floating
value only.
It is used to evaluate
the value of a string
means if the value is
integer it convert it to
integer and if the value
is floating it convert it
to float.
25. Example: Accept two numbers in a variable A,B and display sum of
these two number.
A=input(“Enter the value of A:”)
B=input(“Enter the value of B:”)
print(“A+B=“,A+B
)
-----Output----
Enter the value of A: 10
Enter the value of B: 20
A+B= 1020
It join the value because both are string and + works a
concatenation
26. Example: Accept two numbers in a variable A,B and display sum of
these two number.
A=int(input(“Enter the value of A:”))
B=int(input(“Enter the value of B:”))
print(“A+B=“,A+B
)
-----Output----
Enter the value of A: 10
Enter the value of B: 20
A+B= 30
27. Example: Accept two floating numbers in a variable A,B and
display sum of these two number.
A=float(input(“Enter the value of A:”))
B=float(input(“Enter the value of
B:”))
print(“A+B=“,A+B
)
-----Output----
Enter the value of A: 1.56
Enter the value of B: 1.23
A+B= 2.79
28. eval() is the function used to convert the value automatically on
the basis of value input.
Example:
A=‘25’
B=eval(A)
-----Output----
<class ‘string’>
< class ‘int’>
print(type(A))
print(type(B))
29. If we store string ‘10+20’ in a variable A and need to display the
sum using another variable B
Example:
A=’10+20
’
B=eval(A)
-----Output----
30
print(B)
eval() function convert first the string value
into integer and then read +(plus) sign add
the two values and store in a variable B so B
will be integer type.
30. If we accept the value from user and convert the variable integer or
floating at runtime , so we use eval().
Example:
A=eval(input(“Enter the value:”))
print(type(A)) -----Output----
Enter the value: 15
‘<class int>’
15
print(A)
-----Output----
Enter the value: 5.26
‘<class float>’
5.26
31. Question: Accept the value of two variable A,B from user and print
the variable name and value.
----Output------
Enter the value for A: 10
Enter the value for B: 20
A=10 B=20
A=int(input(“Enter the value for
A:”))
B=int(input(“Enter the value for B:”))
print(“A=“,A,”
B=“,B)
32. Question: Accept the rollno, name and age of a student from user and
display it as shown below:
----Output------
Enter Rollno: 101
Enter name: Amit
Enter Age: 15
Rollno****Name****Age
101 Amit 15
rollno=int(input(“Enter Rollno:”))
nm=input(“Enter Name:”)
print(“Rollno*******Name******Age”)
age=int(input(“Enter Age:”))
print(rollno,”t”,nm,”t”,age)
33. Question: Program to display the output using t
-------OUTPUT-------
1992 17489
1993 29378
1994 100123
1995 120120
Question: Program to using single print() display the following
variable with values
-------OUTPUT-------
Prog=25
Doc=45
Logic=15
Flow=16
print(1992,17489,sep=‘t’)
print(1993,29378, ,sep=‘t’)
print(1994,100123, ,sep=‘t’)
print(1995,120120, ,sep=‘t’)