Before we dig deep into the details of python as a programming language, let's first understand its terminologies and some of the basic principles which will enable us to start building a basic idea which we will be working with moving forward.
In this particular article, we are going to focus on data types we have in python. Let's get started:
Data Types
Data types are the different types of input data accepted by a programming language. They are mostly used in defining, declaring, and storing values/operations.
Most commonly used include: (int, float, str, bool, list, tuple, set, dict, & complex)
When talking about data types in python, I personally would say they are housed under a parent category called standard data types, this parent has children representing different data types:
The parent categories include:
- Numbers - Used to store numeric values.
- Strings - Used to store a sequence of characters.
- Tuple - Used to store a collection of different data types, it is immutable meaning no change happens over time.
- List - Used to store collection of different data types and it is mutable.
- Set - Stores unique elements.
- Dictionary - used to store different data types in the form of key value pairs.
Variables
Variables are are names you can create in python to store values.
myAge = 22
print(myAge)
In the example above we have a variable named myAge which is assigned an integer value of 10, when printing all we need to do is pass in the variable name in the print function.
When using variable in python, here are a few thing to keep in mind:
- They are case sensitive.
- You should not use keywords.
- Must start with a letter or an underscore.
Let's break the standard data types down, and get an understanding of what lies where.
1. Numbers
We have 4 types of numeric data types, that is:
- int - stores whole numbers
- float - store floating point number eg x=35.6
- long - stores higher range of integers eg y=708060L
- complex - although it is not commonly used, it is used to store complex numbers like x=b+5c
2. Strings
Strings can be defined as a sequence of characters usually enclosed in quotation marks either single or double.
#example of strings: "hello", "hey you", "lary mak"
In python 3, Strings are immutable, but it can still be modified with the help of commands like replace() or join().
How to Create Strings
In python strings can be written in 3 ways, that is with either single, double or triple quotes.
my_name = "lary mak" #double quotes
my_bio = 'a growing developer sharing what I learn with the community' #single quotes
more_bio = ''' I believe with help of a community you are able
to achieve a lot of opportunities. Keep practicing and learning''' #triple quotes
String Concatenation
This is combining more than one strings together using the + operator. like below:
first_name = "lary"
last_name = "mak"
print("Hello" + ' ' + first_name + ' ' + last_name) # Hello lary mak
you can not concatenate two different data types. example int with a string, It will result in an error.
String Replication
This allows one to repeat the same string several times. It is done using the * operator.
print("Dev Community" * 10)
This will print Dev Community 10 times in a row.
3. Tuple
It is similar to a list, but in tuple, () are used. It is within the brackets where all the elements are stored. Tuples are immutable.
temp = (36, 33)
4. List
It is a compound data type, meaning it is made of different data types. Lists are stored in [], square brackets. List is used to store an array of items. They are mutable.
student_marks = [9.1, 8.8, 7.5]
5. Dictionary
They are ordered, mutable and stores unique keys as a set. In order for one to create a dict
, {} are used
student_marks = {"Jane":9.1, 8.8, 7.5, "Martha": 6.8, 7.5}
We have covered some of the most commonly used data types in python, in the the next part of this series we will get cover more on Functions $ Conditions.
Incase you missed
See you in the next one!
Connect With me at Twitter | GitHub | YouTube | LinkedIn |
Enjoy Coding ❤.
Top comments (2)
Coding python for 4 years now.
Didn't know about the complex number data type.
I don't know when I would need it but thank you though.
I was suprised too...