Disclaimer: If you are a programmer then you can just skim through this whole section. We will be exploring this section in-depth later. I guess 😉
Variables and Naming conventions
In every programming language, at some point, we need to store some data. This data is stored in our system's memory. To access, use and manipulate this data, we assign them to a variable.
In simple terms, variables are the names you give to computer memory locations that are used for storing the actual data. In python, we can declare the variable anywhere. It is not necessary that all variables should be at the beginning of a code file. Also, to assign a value to the variable we can do the following:
age = 5
name = "John Doe"
height = 5.9
can_speak_french = False
To write better python code, we can follow naming convention as well. Basic rules are:
- Every variable should have a proper meaning
- Try to use short and meaningful names for variables
- If the variable contains more than one meaningful word use underscore ( _ ) to separate them.
In programming, we sometimes store values that won't be changing throughout the execution or lifetime of the program. This kind of data is called constants.
ENVIRONMENT = "DEVELOPMENT"
Constants follow the same naming conventions as any normal variable. But, with minor changes. All constants are written in capital cases only.
Data Types
Data types refer to what kind of data will be stored into the variable or constant. Python can store and use many types of data. Python's built-in data types are categorized as follows:
- Numeric: int, float, complex
- Sequence Type: str, list, tuple
- Boolean
- Set
- Dictionary
Numeric
Python supports three kinds of numeric data. We can store integer (int), float and complex data as numeric data types.
Integer(int)
Integer values can be of any length in python. Example of integers are 10, 2, 29, -20, -150 etc.
a = 5
print("The type of a", type(a))
# OUTPUT: The type of a <class 'int'>
Float
Float is used to store floating-point numbers like 1.9, 9.902, 15.2, etc. It is accurate up to 15 decimal points.
b = 40.5
print("The type of b", type(b))
# OUTPUT: The type of b <class 'float'>
Complex
A complex number contains an ordered pair, i.e., x + iy where x and y denote the real and imaginary parts, respectively.
c = 1+3j
print("The type of c", type(c))
# OUTPUT: The type of c <class 'complex'>
Sequence Type
As the name suggests, this data type stores sequence of values/data. This data type contains the following sub-categories:
- String
- List
- Tuple
String
The string is just a sequence of characters. A single character is also considered as a string in python. To store string data, we can use single, double, or triple quotes.
str1 = 'Use of double quotes'
print(str1)
# OUTPUT: Use of double quotes
str2 = """A multiline
string"""
print(str2)
# OUTPUT: A multiline
# string
str3 = "a"
print(type(str3))
# OUTPUT: <class 'str'>
List
The list data type is similar to an array but not exactly. In an array, we can only store one type of data. Whereas, in python list, we can store multiple types of data into a single list.
list1 = [0, "Python", "Program", 1]
print(type(list1))
print(list1)
# OUTPUT: <class 'list'>
# OUTPUT: [0, "Python", "Program", 1]
list1[0] = 2
print(list1)
# OUTPUT: [2, "Python", "Program", 1]
list2 = list([0, "Python", "Program", 1])
print(type(list2))
print(list2)
# OUTPUT: <class 'list'>
# OUTPUT: [0, "Python", "Program", 1]
list3 = [0, "Python", "Program", 1, 2, 3]
print(list3[2])
print(list3[-2])
# OUTPUT: Program
# OUTPUT: 2
Tuple
The tuple data type is similar to lists in python. The main difference between list and tuple is, once we created a tuple we can not change its value.
tuple1 = (0, "Python", "Program", 1)
print(type(tuple1))
print(tuple1)
# OUTPUT: <class 'tuple'>
# OUTPUT: (0, "Python", "Program", 1, 2, 3)
tuple1[1] = 5
# ERROR:
# Traceback (most recent call last):
# File "main.py", line 14, in <module>
# tuple1[2] = "Python";
# TypeError: 'tuple' object does not support item assignment
Boolean
Boolean is considered as truthfulness. It has only two values.
- True
- False
Note: T and F in True and False must be capital.
a1 = 5
b1 = 5
c1 = a1==b1
print(c1)
# OUTPUT: True
a2 = 5
b2 = "5"
c2 = a2==b2
print(c2)
# OUTPUT: False
a3 = True
print(type(a3))
# OUTPUT: <class 'bool'>
Set
Set can be used the same way we use lists. One difference between set and list is, the list can have duplicate values whereas the set can not. It will be more clear with the example shown below.
To create a Set we have to use the set() function or add items surrounded by curly braces {}. In day-to-day programming, I have not seen much use of the set. Set has very small and specific use-cases.
To create empty set we have to set() function, empty curly braces{} will not work.
list1 = [1, "Python", "Program", 2, 1, 2]
print(list1)
# OUTPUT: [1, "Python", "Program", 2, 1, 2]
set1 = set([1, "Python", "Program", 2, 1, 2])
print(set1)
# OUTPUT: {1, 2, 'Program', 'Python'}
set2 = set("Hello There!")
print(set2)
# OUTPUT: {'h', 'r', 'o', 'H', 'T', 'e', 'l', '!', ' '}
set3 = {1, "Python", "Program", 2, 1, 2}
print(set3)
# OUTPUT: {1, 2, 'Program', 'Python'}
set4 = {}
print(type(set4))
# OUTPUT: <class 'dict'>
set5 = set()
print(type(set5))
# OUTPUT: <class 'set'>
Dictionary
Dictionary is a collection of key-value pairs stored in unorder fashion. If you are from a Java background you can see it as one kind of Map or Hashtable. The items in the dictionary are separated with the comma (,) and enclosed in the curly braces{}.
To create a dictionary using two methods shown in the example:
dict1 = {'Name': 'Python', 1: [1, 2, 3, 4], 2:"DICT"}
print(dict1)
# OUTPUT: {'Name': 'Python', 1: [1, 2, 3, 4], 2: 'DICT'}
dict2 = dict({'Name': 'Python', 1: [1, 2, 3, 4], 2:"DICT"})
print(dict2)
# OUTPUT: {'Name': 'Python', 1: [1, 2, 3, 4], 2: 'DICT'}
dict3 = dict([('Name', 'Python'), (1, [1, 2, 3, 4]), (2,"DICT")])
print(dict3)
# OUTPUT: {'Name': 'Python', 1: [1, 2, 3, 4], 2: 'DICT'}
dict3 = {'Name': 'Python', 'Last':'Programming', 1: [1, 2, 3, 4], 2:"DICT"}
print(dict3['Name'])
print(dict3[1])
print(dict3.get(2))
# OUTPUT: Python
# OUTPUT: [1, 2, 3, 4]
# OUTPUT: "DICT"
In the dictionary, we get or extract data by using the key. So, from the last code when we do dict[1]
, it extracts data with the key 1 that is [1, 2, 3, 4]
.
Conclusion
This section is introduced to just showcase different data types available in python. There are many in-built methods associated with these data types. We will see them in another section where I can show you some practical use of them.
That was it. Thank you for reading.
I know it is not fun to learn about data types but I hope you got some knowledge of data types in Python. If you have any questions reach out to me on Twitter or LinkedIn.
You can subscribe to my Newsletter for more amazing resources and articles.
Top comments (0)