Python Data type & variables: Variables are used to allocate memory location for any data. This means that when we create a variable, we store some space in the memory for that variable. The data can be numeric (any number) or character, (a, b, c… Z), etc. What kind of data will we put in this variable is the data type.
Python Data type & variables: Before using a variable in Python it is not required to declare or specify the type of variable. We know the Python Object Oriented Programming Language. Each of these variables is an object.
The data we store in memory can be of many types. Such as any number, different list, any text, etc. There are five standard data types in Python. They are:
Assigning data to a variable
Python is not a statically typed language. When creating variables in statically typed languages, it is necessary to specify which type of data to hold in the variable. Python doesn’t have to say that way. When we assign data to a variable in Python, it sets the automatic data type.
The = symbol is used to assign a value to a variable. Ex:
name = “Guido van Rossum”
Here name is a variable, to which we have put a name. Which is a string variable.
number = 911
Here I have taken a variable called the number and put 911 in it, which is an integer value. So the number is an integer variable. In the same way
pi = 3.1416
Here pi is a floating-point variable. Because here we put a decimal number.
In Python we can assign the same data to more than one variable at a time. For example:
ovi, niloy, asif = 3.99
Where there are three variables called ovi, niloy, asif, each will have 3.99 value assignments.
We can assign multiple values to multiple variables at the same time if we want. For example:
pi, name, number = 3.1416, “Guido van Rossum”, 999
Where we assign different values to three variables simultaneously. Where pi = 3.1416, name = “Guido van Rossum” and number = 999.
We have seen the variable data type and how to assign data to a variable. Now we know about different data types.
Python supports two types of numbers, integers, and floating points.
To use the integer:
myInt = 10
Above we have taken a variable called myInt and put 10 in it. Assign/assign as a value to a variable.
To use floating point:
myFloat = 10.6
myFloat = float(10) print(myFloat)
If we change the value of an integer variable and place a floating point/decimal value in it, then it will be converted to a floating-point variable automatically. For example
myInt = 10.6
In Python everything is an object. Variables, functions and even code. And one of the objects has an ID, a unique ID, type, and value.
The ID cannot be changed since it is unique. The type is also unique. Type is object class. There are two types of objects. One is the Mutable object. Arcata Immutable Object. The value of a mutable object can be changed. It cannot change the value of an immutable object.
For example, if we want to get the ID of myInt, we will write:
myInt = 10 print(myInt) print id(myInt)
Which will output:
For example, if we want to get the type of myInt, we will write:
myInt = 10 print(myInt) print type(myInt)
Which will output:
If we change the value of myInt to a floating-point number, the type will change:
myInt = 10.6 print(myInt) print type(myInt)
Which will output:
We see that the type has changed.
A little bit of math:
I knew about the number. We can easily figure out how to easily implement them into a program. In Python we can easily add, subtract, multiply, divide.
value1 = 50 value2 = 11 result = value1 + value2 print(result)
value1 = 50 value2 = 11 value3 = value1 - value2 print(value3)
value3 = 50 - 11 print value3
value1 = 50 value2 = 11 value3 = value1 * value2 print(value3)
value1 = 50 value2 = 11 value3 = value1 / value2 print(value3)
Reminder / Reminder:
We are already familiar with the addition, subtraction, multiplication, division sign. I will get acquainted with the symbol, which is:%
We knew this as a positive sign. Its job in programming is to figure out the end of the divide. For example:
value1 = 50 value2 = 11 value3 = value1 % value2 print(value3)
Which will output 6
Now the numbers can be numbered on their own. It can be seen whether it gives exactly the same output.
String / String
Any text, the word is a string. For example:
myString = “Hello world!" print myString
It’s a string. We can write strings within double quotations or single quotations. Written above in double quotations. But we can write as follows
myString = ‘Hello world!' print myString
Writing inside single quotations will be a problem, we cannot write apostrophes.
For example, if you enter the following code, you will see the error in the program:
myString = ‘It’s Friday, YaY' print myString
Not running, is it? That is why we will write the string gulls in double-quotes.
We will take a variable called name. One of which I will write the name of. Then tell the program whose name I am writing, to say hello to him. Such a person’s name is Nahid. The program will print: Hello Nahid, we will write the program as follows:
name = “Nahid" print “Hello “ + name
Above we originally printed two strings together. Hello and Nahid. Another one is called String Concatenation. Or string pairs are given. The two strings are used to append the + sign. Another liberalization:
print “Hello “ + “ World"
Here Hello and World is two separate strings, we have added and printed. This can be done as well.
string1 = "Hello " string2 ="World!" print string1 + string2
There is more work to be done with strings. A lot of work has to do with programming or string in real-life projects. There is a whole section on strings, I will know the details there. We will learn about other data types in the next chapters.