Below are different types of variables in Python.
hl-ipython3
- int
- float
- string
- list
- dictionary
- tuple
#Integer values can be assigned directly using ''=''
age = 10
print(age) #Print function is used to print the value of a variable.
type(age) #We can find out the datatype of a variable using type() function
age #We use Jupyter-notebook to write this tutorial, here just entering a variable will print it's value.
#Any value which contains a decimal point is considered as a float variable.
price = 10.64
price
type(price)
#Float cannot recognize any variables with more than one '.'
ip = 10.0.0.1
#A string should be declared inside a '' or ""
name = 'syam'
type(name)
#Any numbers or alphabets can be saved as a string.
ip = '10.0.0.1'
type(ip)
#Even numbers enclosed in '' or "" will be considered as a string.
age = '31'
type(age)
x = 10
y = 3
#Addition
x + y
#Subtraction
x - y
#Multiplication
x * y
#Division
x / y
#Floor Division - Here the values of variables will be divided but returns only a whole number value.
x // y
#Modulus - Divides and returns the remainder.
x % y
All the above operations can only be done on an integer.
price = '10' #Here 10 is a string
quantity = 5 #Here 5 is an integer
#When a string is multiplied by int, below happens.
price * quantity
#Another example
'a' * 20
x = '10' #A string is declared
y = int(x) #int() will convert string to integer. Here value of y will be integer 10 and value of x will remain str
y
type(y)
x * 5 #Since x is still str, it return below value
int(x) * 5 #x is converted to int and then multiplied with 5
#Len() is used to find the length of a string.
lang = 'malayalam'
len(lang)
#We cannot calculate the length of an integer
atm_pin = 1234
len(atm_pin)
str(atm_pin) #str() can be used to convert integer to string.
len(str(atm_pin)) #So we can now calculate the length of an integer
10 > 5 #Here the output will be boolean (True or False)
5 < 5
5 < 5 or 5 == 5 #if any one of o/p is True, or returns True. == is used to compare if values are equal.
1 <= 1
fruit = 'pineapple'
'apple' in fruit #This will return True because while traversing through pineapple and check for the string apple in it.
'app' not in fruit #This statement is False because string app is present in pineapple
'syam' not in fruit #This statement is True because string syam is not present in pineapple
#This function is used to read values from keyboard input.
input()
name = input("Please enter your name: ") #This will print the message and wait for your keyboard input.
pin = input("Please neter your ATM PIN: ")
type(pin) #All input read via input() will be considered as a string.
#If condition is used to check if a statement is tru or false and execute certain commands respectively.
original = 4556
pin = input("Please Enter your ATM PIN : ")
if int(pin) == original: #Checking if entered pin is equal to value set to variable 'original'
print("Success") #If True it will execute this
print("Dummy Message") #And this and any other line coming with same indentation
else: #If entered pin is not equal, it will execute the commands below this line
print("Failed")
lang = 'Malayalam'
id(lang) #Every string will be assigned a unique id which will be modified when the value of variable changes,
lang = 'English'
id(lang)
#dir() any value will print whatever default modules can be used to it.
dir(lang)
We can use any of the above modules to modify the output of value lang.
lang.upper() #This will traverse through the string and change it's values to UPPER CASE.
lang.lower() #This will traverse through the string and change it's value to LOWER CASE.
lang = 'MalAyalAM'
lang.count('a') #This will count only 'a' in the above variable 'A' will not be considered.
#So to check the correct number of 'a' we need to convert to upper or lower.
lang.lower().count('a')
lang.replace('a','x') #This will replace all 'a' with 'x' (Note : 'A' will not be considered here also)
str.replace? #Adding a ? after any inbuilt function will provide info about it.
#To check if a string ends with a specific value. It will return a boolean value.
conf = '/etc/httpd/conf/httpd.conf'
conf.endswith('.conf')
#To check if a string starts with a specific value. It will also return a boolean value.
conf.startswith('/etc')
#To check if a string is a digit or not.
number = '12345'
number.isdigit()
#If it contains any alphabets, it will return False
number='123gcv'
number.isdigit()
#To check if a string contains only alphabets
name = 'syam'
name.isalpha()
conf = '/etc/httpd/conf/httpd.conf'
conf.isalpha()
#To remove any values from the right side of a string.
#If no arguements are given, white spaces will be removed.
name = ' syam '
name.rstrip()
name.rstrip('am ')
#To remove any values from left side of a string.
#If no arguements are given, white spaces will be removed.
name.lstrip()
#This will search for occurances of a string and remove it from left and right end.
#If no arguements are given, white spaces will be removed.
name.strip()
name.strip(' s')
name = '-------syam--'
name.strip('-')
name = '<color><h1>'
name.strip('<').strip('>')
#The characters in a string is indexed and can be accessed using an index number.
#By default index number starts with 0.
name = 'syam'
name[0]
#We can read the string from backwards as well using '-'
name[-1]
name[-2]
timestamp = '12/Dec/2015:18:25:11'
#We can slice a string using indexes.
timestamp[0:11] #Here it will print from index 0 - 10.
#The same can be done like this also.
timestamp[:11]
#This can also be reversed.
timestamp[12:] #This will print from the 12th index to the end of string.