Python
Variable - DataTypes
#PythonNotes
Variables & Datatypes
Variable in Python
Variable
▪ A variable is a container (storage area) to hold data.
▪ A variable is a name of memory location.
▪ A variable is used to store data.
▪ To indicate the storage area, each variable should be given a
unique name .
▪ A variable value can be changed and it can be reused many times.
4
Akash Technolabs [Link]
Rules for defining variables :-
▪ A variable can have alphabets, digits and underscore. (a-z, A-Z, 0-9, _)
▪ A variable name can start with alphabet and underscore only.
▪ It can't start with digit.
▪ No white space is allowed within variable name.
▪ A variable name must not be any reserved word or keyword e.g. int, float etc.
5
Akash Technolabs [Link]
How to Declare and use a Variable
▪ A variable is a named location used to store data in the memory.
▪ Number
▪ Float
▪ String
6
Akash Technolabs [Link]
Variable Print
is_pass = True
print(is_pass)
7
Akash Technolabs [Link]
Concatenation
▪ We can print Variable and String using Concatenation.
8
Akash Technolabs [Link]
Changing the value of a variable
9
Akash Technolabs [Link]
Assigning multiple values to multiple variables
a = 10
b = 10.5
c = "Akash"
10
Akash Technolabs [Link]
Assign the same value to multiple variables
a = 10
b = 10
c = 10
11
Akash Technolabs [Link]
Sum of 2 number
12
Akash Technolabs [Link]
Data Types in Python
Data Types
▪ A data type specifies the type of data that a variable can store.
14
Akash Technolabs [Link]
Types of Data Types
▪ Python has 6 standard data types namely:-
1. Numbers
▪ Example : 10
2. String
▪ Example : Hello
3. List
▪ Example :[10,20,30]
4. Tuple
▪ Example :(10,20,30)
5. Dictionary
▪ Example :{‘a’:’Akash’}
6. Boolean
▪ Example : True False
7. Sets
15
Akash Technolabs [Link]
String Datatype
▪ Strings are defined either with a single quote or a double quotes.
16
Akash Technolabs [Link]
Single Line & Multiline String
▪ We can use ‘ single or doubtle “ quotes
▪ Use ‘ single or double “ 3 time for multiple line string
17
Akash Technolabs [Link]
String Print and Slicing
18
Akash Technolabs [Link]
Python Type
Conversion
Akash Technolabs [Link]
19
Type Conversion
▪ In programming, type conversion is the process of converting data
of one type to another.
▪ For example: converting int data to str.
20
Akash Technolabs [Link]
Type Conversion
▪ There are two types of type conversion in Python.
1. Implicit Conversion
▪ automatic type conversion
2. Explicit Conversion
▪ manual type conversion
21
Akash Technolabs [Link]
Number Datatype
▪ Number data types store numeric values. Integers, floating point
numbers and complex numbers falls under number datatype.
▪ They are defined as int, float and complex class in Python.
▪ type() function is used to know which class a variable or a value
belongs to.
▪ The isinstance() function is used to check if an object belongs to a
particular class.
Akash Technolabs 22 [Link]
Check DataType using type()
Akash Technolabs 23 [Link]
Check DataType using isinstance()
24
Akash Technolabs [Link]
Implicit Type Conversion
▪ Python automatically converts one data type to another. This is
known as implicit type conversion.
integer_number = 123
float_number = 1.23
new_number = integer_number + float_number
# display new value and resulting data type
print("Value:",new_number)
print("Data Type:",type(new_number))
25
Akash Technolabs [Link]
Implicit Type Conversion
▪ Here C Value automatically converts to float.
a = 10
b = 20.50
c=a+b
print("A Type is",type(a), "Value is", a)
print("B Type is",type(b), "Value is ", b)
print("Sum is ",c)
26
Akash Technolabs [Link]
Explicit Type Conversion
▪ In Explicit Type Conversion, users convert the data type of an object
to required data type.
▪ We use the built-in functions like int(), float(), str(), etc to perform
explicit type conversion.
Akash Technolabs 27 [Link]
Python Type Casting
▪ int() –
▪ constructs an integer number from an integer literal, a float literal (by
removing all decimals), or a string literal (providing the string represents a
whole number)
▪ float() –
▪ constructs a float number from an integer literal, a float literal or a string
literal (providing the string represents a float or an integer)
▪ str() –
▪ constructs a string from a wide variety of data types, including strings,
integer literals and float literals
28
Akash Technolabs [Link]
Type Casting
29
Akash Technolabs [Link]
Check Types
30
Akash Technolabs [Link]
String and Int
31
Akash Technolabs [Link]
Int and Int Type Caste
32
Akash Technolabs [Link]
Number to String Conversion
n1 = 10
n2 = 20.5
n3 = 10+5j
print(n1, type(n1))
print(n2, type(n2))
print(n3, type(n3))
print(n1+n2)
print()
n1 = str(n1)
n2 = str(n2)
n3 = str(n3)
print(n1, type(n1))
print(n2, type(n2))
print(n3, type(n3))
print(n1+n2)
33
Akash Technolabs [Link]
List to Tuple Conversion
value = [10, 20, 30.5, "Hello", "World"]
print("List values")
print(value)
# List to Tuple Conversion
ans = tuple(value)
print("Tuple values")
print(ans)
print(type(ans))
34
Akash Technolabs [Link]
Tuple to List Conversion
value = (10, 20, 30.5, "Hello", "World")
print("Tuple values")
print(value)
# Tuple to List Conversion
ans = list(value)
print("List values")
print(ans)
print(type(ans))
35
Akash Technolabs [Link]
Dictionary to List/Tuple Conversion
mydata = {1: 10, 20.5: 100, 'key': "Hello"}
print(mydata)
print(type(mydata))
print()
# Dictionary to List Conversion
new_list1 = list([Link]())
print(new_list1)
print(type(new_list1))
print()
# Dictionary to Tuple Conversion
new_list2 = tuple([Link]())
print(new_list2)
print(type(new_list2))
print()
36
Akash Technolabs [Link]
Let's Connect SOCIAL MEDIA
#DailyUpdates #FreeCode
@akashsirdotcom @akashpadhiyar
#TechUpdates #FreeTutorials
@akashpadhiyar @akashtechnolabs
Akash Padhiyar #HelpDesk
Tech Educator • Consultant Tech
Mentor • Founder +91 99786 21654
On a Mission to Bridge the
Gap Between Campus & Corporate WEBSITES
[Link] [Link]
akash@[Link]
202, Aarya Arcade, Mithakhali Six Road, Navrangpura, Ahmedabad, Gujarat 380009