Friday , December 6 2024
Python Variable Types - Standard Data Types Numbers String list tuples and Dictionary

Python Variable Types

Python Variable Types

Python Variables

Variables are containers for storing data values. Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.

The interpreter allocates memory and determines what can be stored in the allocated memory depending on the data type of a variable. As a consequence, you can store integers, decimals, or characters in variables by assigning various data types to them.

Every value in Python has a datatype. Different data types in Python are Numbers, List, Tuple, Strings, Dictionary, etc. Variables in Python can be declared by any name or even alphabets like a, aa, abc, etc.

Assigning Values to Variables

In [1]:
x = 10
y = "MachineLearning.org.in"
z = 3.14
print(x)
print(y)
print(z)
10
MachineLearning.org.in
3.14

Variables do not need to be declared with any particular type, and can even change type after they have been set.

In [2]:
x = 10
x = "MachineLearning.org.in"
print(x)
MachineLearning.org.in

Multiple Assignment

In [3]:
a = b = c = 10
print(a,b,c)
10 10 10
In [4]:
a,b,c = 10,3.14,"MachineLearning.org.in"
print(a)
print(b)
print(c)
10
3.14
MachineLearning.org.in

Type Casting

In [5]:
x = str(3)  
print(x)

y = int(3) 
print(y)

z = float(3)
print(z)
3
3
3.0

Get the Type

In [6]:
x = str(3)  
print(type(x))

y = int(3) 
print(type(y))

z = float(3)
print(type(z))
<class 'str'>
<class 'int'>
<class 'float'>
In [22]:
n = input("Enter any value:")
print(n)
print(type(n))
Enter any value:Machine Learning
Machine Learning
<class 'str'>
In [23]:
n = int(input("Enter any value:"))
print(n)
print(type(n))
Enter any value:10
10
<class 'int'>
In [24]:
n = float(input("Enter any value:"))
print(n)
print(type(n))
Enter any value:3.14
3.14
<class 'float'>
In [25]:
n = str(input("Enter any value:"))
print(n)
print(type(n))
Enter any value:Machine Learning
Machine Learning
<class 'str'>

Concatenation

In [7]:
x = "Learning"
print("Machine " + x)
Machine Learning
In [8]:
x = "Machine "
y = "Learning "
z =  x + y
print(z)
Machine Learning 

Python has five standard data types −

  • Numbers
  • String
  • List
  • Tuple
  • Dictionary

1. Number

In [9]:
a = 10
print(a)
10

delete number

In [10]:
del a
In [11]:
print(a)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-11-bca0e2660b9f> in <module>
----> 1 print(a)

NameError: name 'a' is not defined

2. String

In [12]:
s = "MachineLearning.org.in"
print(s)           # Prints complete string
print(s[0])        # Prints first character of the string
print(s[7:15])     # Prints characters starting from 3rd to 5th
print(s[7:])       # Prints string starting from 3rd character
print(s * 2)       # Prints string two times
print('www.' + s)  # Prints concatenated string
print(s[:])        # Prints complete string with range start to end
print(s[::])       # Prints complete string with range start to end with increment 1
print(s[::2])      # Print String with increment of 2
print(s[-1])       # print last character of a string
print(s[-3:])      # print last 3 character of a string
MachineLearning.org.in
M
Learning
Learning.org.in
MachineLearning.org.inMachineLearning.org.in
www.MachineLearning.org.in
MachineLearning.org.in
MachineLearning.org.in
Mcieerigogi
n
.in

3. List

In [13]:
L = [10,3.14,"Machine",45,'Learning',78.98,69,'.org.in']
print(L)
print(L[:])
print(L[::])
print(L[0])
print(L[2:5])
print(L[2:])
print(L[:5])
print(L[-1])
print(L[-3:])
[10, 3.14, 'Machine', 45, 'Learning', 78.98, 69, '.org.in']
[10, 3.14, 'Machine', 45, 'Learning', 78.98, 69, '.org.in']
[10, 3.14, 'Machine', 45, 'Learning', 78.98, 69, '.org.in']
10
['Machine', 45, 'Learning']
['Machine', 45, 'Learning', 78.98, 69, '.org.in']
[10, 3.14, 'Machine', 45, 'Learning']
.org.in
[78.98, 69, '.org.in']
In [14]:
L1 = [10,20,30]
L2 = [40,50,60]
print(L1 + L2)
[10, 20, 30, 40, 50, 60]
In [15]:
L1 = [10,20,30]
print(L1 * 3)
[10, 20, 30, 10, 20, 30, 10, 20, 30]

4. Tuples

In [16]:
T = (10,3.14,"Machine",45,'Learning',78.98,69,'.org.in')
print(T)
print(T[:])
print(T[::])
print(T[0])
print(T[2:5])
print(T[2:])
print(T[:5])
print(T[-1])
print(T[-3:])
(10, 3.14, 'Machine', 45, 'Learning', 78.98, 69, '.org.in')
(10, 3.14, 'Machine', 45, 'Learning', 78.98, 69, '.org.in')
(10, 3.14, 'Machine', 45, 'Learning', 78.98, 69, '.org.in')
10
('Machine', 45, 'Learning')
('Machine', 45, 'Learning', 78.98, 69, '.org.in')
(10, 3.14, 'Machine', 45, 'Learning')
.org.in
(78.98, 69, '.org.in')
In [17]:
T1 = [10,20,30]
T2 = [40,50,60]
print(T1 + T2)
[10, 20, 30, 40, 50, 60]
In [18]:
T1 = [10,20,30]
print(T1 * 3)
[10, 20, 30, 10, 20, 30, 10, 20, 30]

5. Dictionary

In [19]:
d = {}
print(d)
{}
In [20]:
d = {'Name':'Kevin','Age':34,'Marks':89.58}
print(d)
print(d['Name'])
print(d['Age'])
print(d['Marks'])
{'Name': 'Kevin', 'Age': 34, 'Marks': 89.58}
Kevin
34
89.58
In [21]:
print(d.keys())
print(d.values())
print(d.items())
dict_keys(['Name', 'Age', 'Marks'])
dict_values(['Kevin', 34, 89.58])
dict_items([('Name', 'Kevin'), ('Age', 34), ('Marks', 89.58)])

About Machine Learning

Check Also

Python MySQL Insert Into Table

Python MySQL Insert Into Table

4 Python MySQL Insert Into Table Python MySQL Insert Into Table¶To fill a table in …

Leave a Reply

Your email address will not be published. Required fields are marked *