Python Basic Rules #2
Python data types
There are 9 types of python data types :
- String
- Integer
- Float
- Complex Number
- Boolean
- List
- Tuple
- Set
- Dictionary
How to Create a Python String Data Type
In Python, there are 3 ways to create a string data type :
- Using single quotes (‘)
- Using double quotes (‘‘)
- Using one or two quotation marks 3 times (‘ ‘ ‘) or (“ “ “)
Writing regular code uses the string data type
Output :
If you want to separate it into line differences or multiple lines, use an escape character followed by one special character. For example, to write the character ‘tab’ you can use \t, and to create a new line character (move line) you can use the \n sign:
Output :
If you want to concatenate string data :
output :
Strings as Arrays
Output
How to Create a Python Number Data Type
- Integer
Integer (int): An integer data type
Output :
- Float
Decimal / Fraction data type
Output :
- Complex Number
Complex number or imaginary number data type
Output
How to Create a Python Boolean Data Type
The boolean data type is very simple. This data type can only be entered with one of 2 values: True or False. The boolean data type is mostly used for branching program code or for deciding what to do when a condition occurs.
Manual writing
Output
Apart from being inputted manually, boolean data types are more often obtained as a result of comparison operations, such as whether a number is greater than another, is it less, or is it equal to. Here’s an example of using this comparison operation:
Output
How to Create a Python List Data Type
To create a List data type, use square brackets, then each list member is separated by a comma.
Output
Using the type () function, we can confirm that this is the Python list data type:
Ouoput
How to Create a Python Tuple Data Type
The Tuple data type is very similar to the List data type we studied earlier, where the Tuple data type also consists of a collection of other data types. The difference is, the members in the Tuple data type cannot be changed after they are declared.
To create a Tuple data type, use regular brackets, then each list member is separated by a comma. Here’s an example:
Output
How to Create a Python Set Data Type
Python data set is a data type that contains a collection of data types and is used to process the set. When compared to other array data types in Python, data set types to differ in terms of index, sorting, and unique values.
The data set type has no index, so there is no sorting mechanism. As a result of not having an index, we cannot add new values to the data set type by writing the index number (as in the list data type).
How to Create a Python Dictionary Data Type
A data type dictionary is an array data type where the key or index of the array can be in the form of strings, not just numbers. In other programming languages (such as PHP) this dictionary is also known as an associative array.
Output
For the first print to display all data, and the second print to display the desired data values.