Data Science and Machine Learning Internship ...
- 22k Enrolled Learners
- Weekend/Weekday
- Live Class
Python programming language is one of the most sought out programming languages nowadays. Developers want to focus on the implementation part rather than spending time writing complex programs. This is where python actually delivers, with the ease of access and readability. Fundamental concepts are the foundation of any programming language and hence in this blog we will learn the concept of Python variables and Python Data types. Following are the topics covered in this blog:
Gain expertise in scripting in Python and prepare yourself to take up Python job opportunities with Edureka’s Online Python Course Training, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.
Variables and data types in python as the name suggests are the values that vary. In a programming language, a variable is a memory location where you store a value. The value that you have stored may change in the future according to the specifications.
A Python Variable is created as soon as a value is assigned to it. It does not need any additional commands to declare a variable in python.
There are a certain rules and regulations we have to follow while writing a variable, lets take a look at the variable definition and declaration to understand how we declare a variable in python.
Python has no additional commands to declare a variable. As soon as the value is assigned to it, the variable is declared.
x = 10 #variable is declared as the value 10 is assigned to it.
There are a certain rules that we have to keep in mind while declaring a variable:
There are several data types in python. Lets take a look at the python data types.
Every value that we declare in python has a data type. Data types are classes and variables are the instances of these classes.
According to the properties they possess, there are mainly six python data types. Although there is one more data type range which is often used while working with loops in python.
Numerical data type holds numerical value. In numerical data there are 4 sub types as well. Following are the sub-types of numerical data type:
Integers are used to represent whole number values.
x = 100 y = 124 # it will be the integer as long as the value is a whole number.
To check the type of any variable data type, we can use the type() function. It will return the type of the mentioned variable data type.
Float data type is used to represent decimal point values.
x = 10.25 y = 12.30
Complex numbers are used to represent imaginary values. Imaginary values are denoted with ‘j’ at the end of the number.
x = 10 + 5j
Boolean is used for categorical output, since the output of boolean is either true or false.
num = 5 > 4 #num is the boolean variable type(num) #the output will be bool print(num) #this will print true.
Strings in python are used to represent unicode character values. Python does not have a character data type, a single character is also considered as a string.
We denote or declare the string values inside single quotes or double quotes. To access the values in a string, we use the indexes and square brackets.
name = 'edureka' name[2] #this will give you the output as 'u'
Strings are immutable in nature, which means you cannot change a string once replaced.
Command line input for strings
x = input() print( 'hello' , x)
Operations using strings
name = 'edureka' name.upper() #this will make the letters to uppercase name.lower() #this will make the letters to lowercase name.replace('e') = 'E' #this will replace the letter 'e' with 'E' name[1: 4] #this will return the strings starting at index 1 until the index 4.
Now that we have understood numbers and strings, lets understand the relatively complex data types.
List is one of the four collection data type that we have in python. When we are choosing a collection type, it is important to understand the functionality and limitations of the collection. Tuple, set and dictionary are the other collection data type of python.
A list is ordered and changeable, unlike strings. We can add duplicate values as well. To declare a list we use the square brackets.
mylist = [10,20,30,40,20,30, 'edureka']
Accessing values from a list
We use indexes to access values from a string.
mylist[2:6] #this will get the values from index 2 until index 6.
Adding/Replacing values in a list
mylist[6] = 'python' #this will replace the value at the index 6. mylist.append('edureka') #this will add the value at the end of the list. mylist.insert(5, 'data science') #this will add the value at the index 5.
Other operations that we can perform on a list are following:
Method Name | Property |
clear() | removes all the elements from the list |
copy() | returns a copy of the list |
extend() | add the elements of the list to the end of the current list |
count() | returns the number of elements of the specified value |
index() | returns the index of the element |
pop() | removes the element from the specified position |
remove() | removes the item with the specified value |
sort() | sorts the list |
reverse() | returns the reversed list |
Lists can store any data type as items. Be it numbers, strings or any other data type as well.
a = [10,20,30] b = [60 , 50 , 40, a] #to access a value from the list a we can write b[3][2] #this will return 30 as output.
Lets understand the next collection data type in python i.e tuples.
Tuple is a collection which is unchangeable or immutable. It is ordered and the values can be accessed using the index values. A tuple can have duplicate values as well. To declare a tuple we use the round brackets.
mytuple = (10,10,20,30,40,50) #to count the number of elements mytuple.count(10) #the output will be 2 #to find the index mytuple.index(50) #the output will be 5. since the index number at 50 is 5.
Since a tuple is unchangeable once you have declared it, there are not many operations you can perform on a tuple. But there is a bright side to using a tuple, you can store values in a tuple which you do not want to change while working in a project. Although you will be able to access the values, but there will not be any changes to be made.
A set is a collection which is unordered, it does not have any indexes as well. To declare a set in python we use the curly brackets.
myset = {10, 20 , 30 ,40, 50, 50}
A set does not have any duplicate values, even though it will not show any errors while declaring the set, the output will only have the distinct values.
To access the values in a set we can either loop through the set, or use a membership operator to find a particular value.
for x in myset: print(x) #this will get all the values. 20 in myset #this will return true if the value is in the set. #to add a value in a set myset.add('edureka') #to add multiple values in a list myset.update([ 10, 20, 30, 40, 50]) #to remove an item from a set myset.remove('edureka') #we can use the discard or pop method to remove an item from a set as well. myset = {10, 20, 30} myset1 = {10,30,50} myset.issubset(myset1) #this will return false myset.union(myset1) #this will return a set with the union of the two sets.
Method Name | Property |
clear() | clears the items from a set |
copy() | returns the copy of the set |
difference() | returns a set with the difference of the two sets |
isdisjoint() | returns if the sets have intersection |
issubset() | returns if the set is a subset |
symmetricdifference() | returns a set with the symmetric difference |
update() | update the sets with union of the set |
Lets take a look at another collection data type which has key value pairs.
A dictionary is just like any other collection array in python. But they have key value pairs. A dictionary is unordered and changeable. We use the keys to access the items from a dictionary. To declare a dictionary, we use the curly brackets.
mydictionary = { 'python': 'data science', 'machine learning' : 'tensorflow' , 'artificial intelligence': 'keras'} mydictionary['machine learning'] #this will give the output as 'tensorflow' mydictionary.get('python') #this serves the same purpose to access the value.
Since we are using the keys to access the items, they cannot be duplicate.The values can have duplicate items.
Data Manipulation in a dictionary
#adding a new value mydictionary['analysis'] = 'matplotlib' #replacing a value mydictionary['analysis'] = 'pandas' #deleting a value mydictionary.pop('analysis') #remove() , del also serves the same purpose for deleting a value.
Other operations in a dictionary include the following.
Method Name | Property |
copy() | returns a copy of the dictionary |
clear() | clears the dictionary |
items() | returns a list containing tuple of key value pairs |
keys() | returns a list containing all the keys |
update() | updates the dictionary with all the key-value pairs |
values() | returns a list of all the values in a dictionary |
setdefault() | returns the value of a specified key |
Range is a data type which is mainly used when we are using a loop. Lets take an example to understand this.
for x in range(10): print(x) #this will print the numbers from 0-10. Range will have the numbers from 0-10
Now that we have understood different python data types, there is another important concept of type casting which is helpful when we change from one data type into another. Lets understand the concept of type casting.
Type casting basically is the process of changing one data type into another. We have constructors for each of the data types in python data types.
We can simply use these constructors to use the specified data type or we can change a data type to another using these constructors. Lets understand this with an example.
a = [ 10 , 20 ,30,40] #to change this list into a tuple i can simply write tuple(a) #now the list will change to a tuple.
Using these constructors we can use various data types with the functionality of the other. Suppose we declare the list mentioned in the example as a tuple in a program, it will become immutable for that particular operation. Similarly we can use other constructors as well.
Now that we have discussed Python variables and Python data types. I hope the properties of each data type and the operations are clear to you. If you want to kick-start your learning in python programming, you can refer to the Edureka’s Data Science with Python Course for python programming. The curriculum is top notch and contains structured learning to master python.
If you have any questions, write them in the comment section. We will get back to you.
Related FAQ
Course Name | Date | Details |
---|---|---|
Data Science with Python Certification Course | Class Starts on 14th December,2024 14th December SAT&SUN (Weekend Batch) | View Details |
edureka.co