Day 3 : Complete Python Course for CBSE 9th 10th 11th and 12th Students
Variables in Python
In any programming language variable is a named memory location to store data. Simply to say it is a container to store data and can be changed at any time as per requirement during program execution. In case of programming languages lice C, C++ and Java we have to declare variables before using them and a certain type of variable can store a certain type of value only. But in case of Python variables they are Statically Typed or Loosely Typed.
Remember the below points in case of Python variables :
- The Python variable is created at the moment we assign any value to it. No need of separate declaration statement.
- The data type of variable is not fixed. The variable storing one type of data at this moment can store another type of data at another moment. This is possible because in Python we do not assign values to variables, instead we assign reference of the object or value (because in python every thing is treated as an object) to the variable.
How to assign value to variable in Python ?
We can use assignment operator =
to assign value to variable in Python. Please look at the below example to get clear idea.
Example :
x = "Subrat Dash" # In this case x is a variable which stores string value print("Value of x is : ",x) # Value of variable x is printed on screen print("Type of x is : ",type(x)) # Returns the data type of value stored in variable. The output will be <class 'str'> x = 10 # x is reassigned a value which is numeric. So x now is a numeric variable print("Value of x is : ",x) print("Type of x is : ",type(x))# Returns the data type of value stored in variable. The output will be <class 'int'> x = y = 10 # Two variables x and y assigned same value print("Value of x is : ",x); print("Value of x is : ",y) x , y = 10, 15 # Two variables x and y assigned different values print("Value of x is : ",x); print("Value of x is : ",y)
How to take input value from user for the variable in Python ?
input() function of Python can be used to take input value from user and store it to a Python variable.
Syntax : variable name = input (prompt)
print() function of Python can be used to output any value on screen. Multiple expressions can be used in print statement to output on screen by separating them by commas.
Example :
x = input("Enter Your Name : ") y = input("Enter Your Enrollment Number : ") print("You are, ",x," and Your Enrollment Number is ",y)
What are the rules and naming conventions for variables in Python ?
The below list of rules you should remember while using or creating a new variable.
- Variable names can contain a combination of letters, numbers and underscore.
- Variable name can not start with a number.
- Variable name can not contain special characters like @,#,!,%,$ etc.
- Generally if we want a variable name with two words, we can separate them with underscore.
Example :
myName = "Subrat Dash" #Valid Syntax my_Name = "Subrat Dash"#Valid Syntax address1 = "India"#Valid Syntax 1address = "India"#Invalid Syntax
Data Types in Python
Data type in Python represents the kind of value and the type of operation those can be performed on them. Since every thing is an object in Python so data types are classes and variables are instances of those classes. We have already seen this on our previous example. When we are checking type of a variable it is showing <class ‘int’>. Means that variable is an instance of int class or simply we can say the variable is of int data type.
Numeric Data Type in Python
These type of data holds a numeric value, which may be an integer, float or complex number.
Example :
x = 5 print("x contains value ",x," and is an instance of ",type(x)) x = 5.0 print("x contains value ",x," and is an instance of ",type(x)) x = 5j print("x contains value ",x," and is an instance of ",type(x))
Boolean data type in Python
These data types can store only true values, either true or false.
Example :
x = 7 > 5 print("x contains value ",x," and is an instance of ",type(x)) x = 3 > 5 print("x contains value ",x," and is an instance of ",type(x))
Dictionary Data Type in Python
A Dictionary in Python can be created by placing a sequence of items with in curly braces { }
, separated by commas, each item being a pair in the form key:value
and key and value can be of any data type. It is unordered, can be changed and has no duplicate entries( has unique keys but the values in the keys can be duplicated).
The below example will solve all the following queries related to dictionary data type of python, like :
- How to print a dictionary data type ?
- How to access the element value of dictionary data type in python using key ?
- How to access the element value of dictionary data type in python using get ?
- How to update the value of an item of dictionary data type in python ?
- How to remove a specific item from dictionary data type in python using key ?
- How to remove last item of dictionary data type in python ?
- How to add a new item to python dictionary data type ?
Example :
x = {'name':'Subrat Dash','roll':12,1:12345} print(x) print(len(x)) # Number of items in dictionary print('name' in x) # checks ifname
is a key in dictionaryx
print(x['name']) #Accessing the item using key print(x.get('roll')) #Accessing the item using get print(x[1]) x['name']="Subrat Sir" #Updating the value of item print(x['name']) x.pop(1) #removing an item using key print("After pop : ",x) x.popitem() #removing last item print("After popitem : ",x) x.popitem() print("After popitem : ",x) x['name']="Subrat Kumar Dash" #adding a new item print("After adding a new item :",x)
Set data type in Python
A set is also a collection of items like dictionary but do not have key value pairs. A set can be created using built in set() function or by placing a sequence inside curly braces separated by commas. It is unordered and no duplicate entries are present. Set does not have indexes. Most of the operations that can be done on set in Mathematics can also be done in Python.
The below example will solve following queries :
- How to create a set in Python ?
- How to print a set in Python ?
- How to print number of elements in a set in Python ?
- How to add element to a set in Python ?
- How to remove element from a set in Python ?
- How to use subset, superset, union, intersection, difference and other mathematical set operations in Python ?
Example :
x = set("Subrat") # method to create a set print(x) # printing a set print(len(x)) # Number of elements in a set x.add(15) #adding element to set print(x) x.add('a') print(x) x.add(('Z','t')) print(x) x.update(['z','t','k']) # Updating a set print(x) x.pop() # Removes a random item from set print(x) y={"r","a","t"} # another method to create a set print(y) print(y<=x,y.issubset(x)) #issubset() and <= returns same result print(y>=x,y.issuperset(x)) #issuperset() and >= returns same result z = set("kpt") print(x|z, x.union(z)) #union() and | returns same result print(x&z, x.intersection(z)) #intersection() and & returns same result print(z-x, z.difference(x)) #difference() and - returns same result print(z^x, z.symmetric_difference(x)) #symmetric_difference() and ^ returns same result print(z.isdisjoint(x)) #isdisjoint() has no equivalent operator print(z) print(z-{'k'}) # Can be used to remove specific elements print(z-set('p')) # Can be used to remove specific elements
Sequence data type in Python
We will discuss this data type in next class.
Click Here to Subscribe TTRC Player and get recorded downloadable video tutorials of this course.
Tag:complete python course for cbse 11th students, complete python course for cbse class 12th students, complete python course for CBSE students, complete python course for CBSE students in Hindi, complete python tutorial in hindi, data types in python, hindi python tutorial, python tutorial in hindi, variables in python
You must log in to post a comment.