PREV NEXT
- Before going to discuss data types in python programming language, we need to know about few points. In python programming language, we are not going to use any built-in data types to declare a variable like in C, C++. In python, mostly we use built-in data types for type casting the variables.
- All data values are treated as object for related classes in python programming Language and every object as an identity, value and type.
- To determine a variable’s type in Python, you can use the type () function.
- The value of some objects can be changed. Objects whose value can be changed are called mutable.
- objects whose value is unchangeable are called immutable. Below is the details of Python data types
Data Types in Python:
- Numbers
- Strings
- List
- Tuple
- Dictionary
- Set
1. Numbers:
In python, majorly we can divide numbers into three types. They are,
Integers, floating point numbers and complex numbers,
- Integer numbers represents whole numbers either negative or positive numbers
- Floating point numbers represents fractional part either negative or positive numbers
- To check the type of data types, enter into interpreter mode and then enter below commands one by one
2. Strings:
- Strings are nothing but collection characters in sequence
- Strings are immutable
- We can write strings in python in following three ways.
Single quotes:
If character holds in a single quotes, then we can call it as string.
Double quotes:
If character holds in a double quotes, then we can call it as string.
Triple quotes:
If character holds in triple quotes, then we can call it as string. But, this type of strings we can use for doc and for multi line comments.
Note:
Combination of double and single quotes will be accepted, but it should not start with double quote and end with single quote vice versa.
-
Normal Strings
-
Combination Strings
Special character in strings:
Escape Sequence |
Meaning |
\’ | Single Quote |
\” | Double Quote |
\\ | Back Slash |
\t | Horizontal Tab |
\n | New line |
Note:
For complete and advanced string concepts, we will discuss in upcoming tutorial.
3. Lists:
- List is a container which holds comma-separated values i.e elements or items with in holds the square brackets []
- Elements might be same types or combination of different types
- We can access the elements by using the index number
We can create the empty list by using without elements
1 2 3 4 5 |
>> list_empty=[] >> print list_empty [] |
4. Tuple:
- Tuple is a container which holds comma-separated values i.e elements or items with in holds the parenthesis (), they are immutable i.e we cannot change the content once we created
- Elements might be same types or combination of different types
- We can access the elements by using the index number
We can create the empty tuple by using without elements
1 2 3 4 5 |
>> tuple_empty=() >> print tuple_empty () |
5. Dictionary:
- Dictionary is a container which holds comma-separated values with pair i.e elements or items with in holds the curly braces {} key: value pairs where keys and values, keys are immutable
- To Access the objects in dictionary we can use keys, keys might be string, number and tuple
- Elements might be same types or combination of different types
- We can access the elements by using the keys
- We can create an empty dictionary by using empty curly braces
Syntax:
1 |
Dict={key1:2,”key2”:4,key3:”str”} |
6. Sets:
- A set is an unordered collection of unique elements. Basic uses of sets is to deal with set theory.
- It supports mathematical operations like union, intersection, difference, and symmetric difference or eliminating duplicate entries.