Python Programming (123 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (30 Blogs)
  • Data Science (39 Blogs)
  • Mastering Python (66 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

What Are Variables And Data Types In Python?

Last updated on May 22,2019 1.8K Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

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 variables and data types in python. Following are the topics covered in this blog: 

What Are Variables In Python?

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.

variables-variables and data types in python-edureka

A Variable in python 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.

Variable Definition & Declaration

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:

  1. The variable name cannot start with a number. It can only start with a character or an underscore.
  2. Variables in python are case sensitive.
  3. They can only contain alpha-numeric characters and underscores.
  4. No special characters are allowed.

There are several data types in python. Lets take a look at the data types in python.

Every value that we declare in python has a data type. Data types are classes and variables are the instances of these classes.

Data Types In Python

According to the properties they possess, there are mainly six data types in python. Although there is one more data type range which is often used while working with loops in python.

types of data types-variables and data types in python-edureka

Numerical Data Types

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: 

  1. Integers
  2. Float
  3. Complex Numbers
  4. Boolean

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

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.

indexes-variables and data types in python-edureka

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.

Lists

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 is 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.

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.

Sets

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 NameProperty
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.

Dictionary

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 NameProperty
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

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 data types that we have in python, 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

Type casting basically is the process of changing one data type into another. We have constructors for each of the data types in python.

  1. list()
  2. set()
  3. tuple()
  4. dict()
  5. str()
  6. int()
  7. float()

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 variables and data types in python. 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 certification program 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.

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.