Variables, Numbers, Strings and Operators in python
We talked about python programming language in our previous post. Now in this article we will see how to declare variables, define numbers and strings, the use of operators in python. In python language, we can declare a variable using just a simple character.
x=5
y=1.0
z=1j
a=”b”
b=”banana”
Those are the variables we can create on python. Now we need to know, which is what kind of variable. Here the first one is an integer. Second one is float. Third one is a complex number fourth one is a character and the last one is a string. As we described in my previous article that we don’t need to define the variable type here so we can see that we assigned the variables without declaring any type of the variable.
In python we can declare three different kinds of numbers. Those are integer, float and complex. If we take a close look to the first three variables we can easily understand that those are the number types those are supported by python. Here we can find the type of the numbers by using the function type(). We have to provide the variable name as an argument of the type function. If we print those type we will see the results below.
>>> print(type(x))
<type ‘int’>
>>> print(type(y))
<type ‘float’>
>>> print(type(z))
<type ‘complex’>
Well next we will talk about character and strings. We have to declare the values within double quotes. When we declare the value within double quotes then it will be considered as string or in another word we can say that this will be a array of characters. So anything within double quote will be considered as a character or a set of characters. The last two variables are examples of this type.
Next we will talk about operators. As we know in all kind of programming there are different kinds of operators. I am not going to describe all those in here. Here we are trying to bring up only the necessary things that we may need in our real life work. According to that we are going to need few mathematical operators, few assignment operators, few logical and comparison operators.
Let’s being with simple mathematical operators we usually use.
+(Plus)
-(subtract )
*(multiply)
/(division)
Above four simply mean the simple mathematic operations. We still have 3 special mathematical operators.
% (modulus)
** (exponential)
//(floor division)
The first one we can use to find the end result of division. Other two are used in special cases.
Let’s begin with the some assignment operators.
=(value assigning operator)
+=(variable plus the value equals to new value)
-= (variable subtract the value equals to new value)
*=( variable multiplies the value equals to new value)
/=( variable divides the value equals to new value)
Above five operators can be used is so many cases depending on the necessity. But the first one is used all the time for assigning a value to the variable.
Well logical operators are and, or, not etc. these operators usually used in conditions for making a logic simpler and easier to write and read. We usually use these logics on base of two variables or on two different comparisons. One example is given below.
x<20 and x>35
x<20 or x>35
not(x <20 or x > 10)
now we have only comparison operators. These are kind of very few and easy to learn.
== (equal)
>= (greater than or equal to)
<= (less than or equal to)
!= (not equal)
These above are the most commonly used comparison operators in python.
Well there are a lot left to learn but we should learn those step by step. So I will bring those up points in future articles.