5.Data types
The types of data available in Python are:
Using variables:
Unlike languages like Java and C #, where we had to specify the variable when defining a variable, it is enough for Python to just write the variable name and assign it a value by the equal sign:
variableName = Value
The following example illustrates how to define and quantify variables:
1 intVar = 10
2 floatVar = 12.5
3 boolVar = True
4 StringVar = "Hello World!"
5 listVar = [1,5,8]
6 tupleVar = ("Python","Programming","begginer")
7 dictionaryVar = {'Name': 'jack', 'family': 'Scalia', 'Age': 7}
8
9 print("intVar = {0}" .format(intVar))
10 print("floatVar = {0}" .format(floatVar))
11 print("boolVar = {0}" .format(boolVar))
12 print("StringVar = {0}" .format(StringVar))
13 print("listVar = {0}" .format(listVar))
14 print("tupleVar = {0}" .format(tupleVar))
15 print("dictionaryVar = {0}" .format(dictionaryVar))
Result:
intVar = 10
floatVar = 12.5
boolVar = True
StringVar = Hello World!
listVar = [1, 5, 8]
tupleVar = ('Python', 'Programming', 'begginer')
dictionaryVar = {'Name': 'jack', 'family': 'Scalia', 'Age': 7}
In lines 7-1, variables are defined. But what is the type of these variables? Python considers the type of variables depending on the value assigned to them.
For example, the StringVar variable type in line 4 is of the string type, because a string value is assigned to it. Notice lines 5, 6 and 7 above. In line 5, a variable is defined and the data type assigned to it is list type.
As mentioned in the previous lesson, the list [] is used to define the list and the comma separated items are separated by a comma:
listVar = [1, 5, 8]
In line 6, a variable is defined and a value of type tuple is assigned to it. The tuple definition uses () instead of []. We will explain the difference between the two in future lessons. But in line 7 a dictionary is defined. To define the dictionary between the key and the value of the mark: and between the key / the value of the mark:
dictionaryVar = {Key1:Value1, Key2:Value2, Key3:Value3}
For example, in the example above we have defined a dictionary that has three items or keys / values, with a comma (,) between them. But between a key and the value associated with it: To assign a value to several variables you can do the following:
identifier1 = identifier2 = ... indentifierN = Value
Consider the following example:
num1 = num2 = num3 = num4 = num5 = 10 message1 = message2 = message3 = "Hello World!" print(num1) print(num4) print(message1) print(message3)
Result:
10 10 Hello World! Hello World!
Note that for the variables defined above, a memory house is allocated, ie 10 is stored in memory and variables num1, num2, num3, num4 and num5 refer to that memory memory. You can also define several variables and specify a separate value for each of them:
identifier1, identifier2, ... indentifierN = Value1, Value2, ... ValueN
Consider the following example:
num1, num2, message1 = 10, 12.5, "Hello World!" print(num1) print(num2) print(message1)
Result:
10 12.5 Hello World!
In the above code, num1 equals 10, num2 equals 12.5 and message1 equals Hello World! In Python, variables must both be defined and quantified.
That is, if you define a variable and do not assign a value to it and run the program you will encounter an error:
number print(number)
Result:
Traceback (most recent call last): File "...", line 1, in
As we mentioned in the previous lesson, a string is essentially a set of characters that are inside the "" or '' sign. Each of these characters has an index by which the index is accessible. Character indexes in the string start from 0. Notice the following:
message = "Hello World!"
In the above string the character index O is 4. Consider the following:
H e l l o W o r l d ! 0 1 2 3 4 5 6 7 8 9 10 11
Now to print a character (for example W) from this string, just do the following:
message = "Hello World! " print(message[6])
Result:
W
As you can see in the code above, just type the variable name, in front of it a pair of brackets, and inside the brackets, write the index of the character we want to print. The same applies to indexes in List and Tuple:
listVar = [1, 5, 8]
tupleVar = ("Python", "Programming", "begginer")
print(listVar[2])
print(tupleVar[1])
Result:
8 Programming
And in the dictionary, you have to write the key name to show it to you:
dictionaryVar = {'Name': 'jack', 'Family': 'Scalia', 'Age': 7}
print(dictionaryVar['Family'])
Result:
Scalia
The point to make here is that the keys / values in the dictionary can be of any type, and you need to type the key name exactly to print the value for a key. Consider the following example:
dictionaryVar = {1:'Jack', '2':'Scalia', 3:7}
print(dictionaryVar['2'])
Result:
Scalia
In the example above we have printed the value of the '2' key. Now if we write 2 instead of '2', that is, don't leave a quotation mark, we get an error:
dictionaryVar = {1:'Jack', '2':'Scalia', 3:7}
print(dictionaryVar[2])
Result:
Traceback (most recent call last): File "C:/MyFirstProgram.py", line 3, in
(PlaceHolders)
Note the print () function in lines (15-9). This function is divided into two parts.
The first part is a formatted string, and the second part contains a function called format () that has a value or values used by the formatted string.
If you look closely, the formatted string has a zero that is enclosed within two braces. However, the number inside the two curls can range from zero to n.
These are called fatal numbers. These numbers are replaced by values or values inside the format () function. For example, {0} means that the first value is placed inside the format () function.
To clarify, consider the following:
print("The values are {0}, {1}, {2}, and {3}.".format(value1, value2, value3, value4))
Sidebars start at zero. The number of arguments must be equal to the number of values entered inside the format () function. For example, if you have four such arguments as above, you should consider four values for them after the formatted string. The first placeholder is replaced with the first placeholder and the second placeholder with the second placeholder. At first it's hard to grasp this concept for those just starting out, but you'll see many examples in future lessons.