Python Variables & Data Types
Python Variable:
Variables are used to store values. In python, there is no explicit declaration for variables. If you pass an integer value to a variable it will automatically create integer variable same with other formats also.
Example:
a= 1
b=2.5
c="test"
print(a)
print(b)
print(c)
print(type(a))
print(type(b))
print(type(c))
Output:
1
2.5
test
<class ‘int’>
<class ‘float’>
<class ‘str’>
Python Data Types:
In python we have mainly 5 data types
• Numbers
• Strings
• Lists
• Tuples
• Dictionaries
Python Numbers:
We have 3 types of numbers in python, if we assign value to them, if will convert to that datatype automatically.
1. Integer
2. Float
3. Complex
Python Integers:
Integer is a Number. You can define Positive, Negative Integer numbers with decimals
Example:
a= 100
print(a)
print(type(a))
a= -100
print(a)
print(type(a))
Output:
100
<class ‘int’>
-100
<class ‘int’>
Python Float:
Float number is a fractional value. You can pass positive and negative numbers.
Example:
a= 10.555555
print(a)
print(type(a))
a= -10.5758585
print(a)
print(type(a))
Output:
10.555555
<class ‘float’>
-10.5758585
<class ‘float’>
Python Complex Numbers:
Complex is a number with equation a+bj . a and b are the real values of graph. J is an imaginary number. We can represent with positive and negative numbers.
Example:
c= 10+5j
print(c)
print(type(c))
c= -10+5j
print(c)
print(type(c))
Output:
(10+5j)
<class ‘complex’>
(-10+5j)
<class ‘complex’>
Python Strings:
String is a representation of continuous characters. In Python, You can define by using single quotes and double quotes. You can find substring by using slice operator [:]. The string index is starting from 0. You can concatenate string by using + operator and * used to repeat the strings.
Example:
Str = 'I love python'
print(Str)
print(Str[0])
print(Str)
print(Str[:6])
print(Str[6:])
print(str+str)
print(Str+' '+ Str)
print(Str *2)
Output:
I love python
I
I love python
I love
Python
Python
I love python I love python
I love pythonI love python
Python Lists:
Python list is versatile sequence data type, you can store integer, float, character in a single list. Same like arrays it index is also zero. You can use slice [:] operator to call subsets. You can concatenate lists by using + operator and repeat by using * operator.
Example:
Str = ['I', 'love', 'python', 'version','3.6']
print(Str)
print(Str[0])
print(Str)
print(Str[:2])
print(Str[2:])
print(Str+Str)
print(Str * 2)
Output:
[‘I’, ‘love’, ‘python’, ‘version’, ‘3.6’]
I
[‘I’, ‘love’, ‘python’, ‘version’, ‘3.6’]
[‘I’, ‘love’]
[‘python’, ‘version’, ‘3.6’]
[‘I’, ‘love’, ‘python’, ‘version’, ‘3.6’, ‘I’, ‘love’, ‘python’, ‘version’, ‘3.6’]
[‘I’, ‘love’, ‘python’, ‘version’, ‘3.6’, ‘I’, ‘love’, ‘python’, ‘version’, ‘3.6’]
Python Tuples:
Python tuples are same like lists. The only difference is it is read only lists. It can’t update. You can define by using Parenthesis operator ()
Example:
Str = ('I', 'love', 'python', 'version','3.6')
print(Str)
print(Str[0])
print(Str)
print(Str[:2])
print(Str[2:])
print(Str+Str)
print(Str * 2)
Output:
(‘I’, ‘love’, ‘python’, ‘version’, ‘3.6’)
I
(‘I’, ‘love’, ‘python’, ‘version’, ‘3.6’)
(‘I’, ‘love’)
(‘python’, ‘version’, ‘3.6’)
(‘I’, ‘love’, ‘python’, ‘version’, ‘3.6’, ‘I’, ‘love’, ‘python’, ‘version’, ‘3.6’)
(‘I’, ‘love’, ‘python’, ‘version’, ‘3.6’, ‘I’, ‘love’, ‘python’, ‘version’, ‘3.6’)
If you forcefully try to change values in lists it will produce the following error
Example:
Str[0] =’test’
TypeError: ‘tuple’ object does not support item assignment
Python Dictionaries:
Python dictionaries are unordered data types which come with key and value pairs. We can call values by using keys. The keys can be numbers and strings. Dictionaries are defined by using {}.
Example:
Dictionary = {"name":"python", "version":"3.6"}
print(Dictionary)
print(Dictionary['name'])
print(Dictionary['version'])
Output:
{‘name’: ‘python’, ‘version’: ‘3.6’}
python
3.6