5.Data types

5.Data types

The types of data available in Python are:

No alt text provided for this image

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))
No alt text provided for this image

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.

To view or add a comment, sign in

More articles by Mohammad Moradi (He/Him)

  • 8.Getting user input

    Python provides the input () method to retrieve user input. As the name implies, it reads all the characters you type…

  • 7.Phrases and Operators

    Learn two words first: Operator: are symbols that perform specific actions. Operand: The values that the operators…

  • 6.Convert data types

    In Python, it is possible to convert one type to another, so-called Type Casting. Python has a set of predefined…

  • 4.Control characters

    Control characters are composite characters that begin with a backslash (\) followed by a letter or number and…

  • 3.Build a simple application

    Let's write a very simple program in Python. This program displays a message.

  • 2.Download and install Python 3.8

    There are various development environments or IDEs for programming in different languages that help programmers write…

  • 1.What is Python

    Python is a versatile, object-oriented and open source programming language designed by Guido van Rossum in 1991 in the…

  • the mark @ in C#

    The @ mark allows you to override the control characters and create a more natural, readable string. When using control…

  • 31.Class as field

    Like all the variables we have used so far, one class or structure can be converted to another class member variable…

  • 30.Nested class

    Sometimes it is necessary to define a class in another class. The most important reason for this is to limit the scope…

Explore content categories