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
x = 10
y = "MachineLearning.org.in"
z = 3.14
print(x)
print(y)
print(z)
Variables do not need to be declared with any particular type, and can even change type after they have been set.
x = 10
x = "MachineLearning.org.in"
print(x)
Multiple Assignment¶
a = b = c = 10
print(a,b,c)
a,b,c = 10,3.14,"MachineLearning.org.in"
print(a)
print(b)
print(c)
Type Casting¶
x = str(3)
print(x)
y = int(3)
print(y)
z = float(3)
print(z)
Get the Type¶
x = str(3)
print(type(x))
y = int(3)
print(type(y))
z = float(3)
print(type(z))
n = input("Enter any value:")
print(n)
print(type(n))
n = int(input("Enter any value:"))
print(n)
print(type(n))
n = float(input("Enter any value:"))
print(n)
print(type(n))
n = str(input("Enter any value:"))
print(n)
print(type(n))
Concatenation¶
x = "Learning"
print("Machine " + x)
x = "Machine "
y = "Learning "
z = x + y
print(z)
Python has five standard data types −¶
- Numbers
- String
- List
- Tuple
- Dictionary
1. Number¶
a = 10
print(a)
delete number¶
del a
print(a)
2. String¶
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
3. List¶
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:])
L1 = [10,20,30]
L2 = [40,50,60]
print(L1 + L2)
L1 = [10,20,30]
print(L1 * 3)
4. Tuples¶
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:])
T1 = [10,20,30]
T2 = [40,50,60]
print(T1 + T2)
T1 = [10,20,30]
print(T1 * 3)
5. Dictionary¶
d = {}
print(d)
d = {'Name':'Kevin','Age':34,'Marks':89.58}
print(d)
print(d['Name'])
print(d['Age'])
print(d['Marks'])
print(d.keys())
print(d.values())
print(d.items())