1 - Declaration of variables in python

A variable is a kind of virtual box in which we can put one (or more) data (s). The idea is to temporarily store data to work with. For your machine a variable is an address that indicates the location of the RAM where the information we have linked is stored.
Python is a dynamically typed language, which means you do not have to declare the type of the variable. So to declare a variable in python, we directly introduce its name followed by its value:

Example

>>> x = 5
>>> # here we introduce a variable whose value is 5
>>> x
5

2 - Variable names and reserved words

Under Python, variable names must follow a few simple rules:

  • A variable name is a sequence of letters a..z, A..Z and digits 0 .. 9, which must always begin with a letter.
  • Only ordinary letters are allowed. Accented letters, cedillas, spaces, special characters such as $,  #, @, etc. are prohibited except for the _ character.
  • The case is significant (uppercase and lowercase characters are distinguished).

Warning ! : Phone, phone, PHONE are therefore different variables. Be careful !

In addition to these rules, it must be added that you can not use as variable names the 29 "reserved words" below (they are used by the Python language itself):

and, assert, break, class, continue, def, del, elif, else, except, exec, finally, for, from, global,  if, import, in, is, lambda, not, or, pass, print, raise, return, try, while, yield

3 - Types of variables

In python a variable is dynamically typed, ie by introducing a variable followed by its value, Python detects its type:

Example

>>> h = "Hello"
>>> h
Hello


In this example Python detects that this is a string whose value is "Hello"

  • Integer : as its name indicates an integer is a digit without decimals.
  • Floats or number with commas: example: 1.5
  • Strings or  string of characters : to simplify everything that is not a number.

There are many others but, to simplify the task we will not mention all types here.

Note 

To know the type of a variable, you can use the  "type ()"  function

Example

>>> x=5
>>> # Here we define a varible of Integer type
>>> type(x)
class int=""
>>> a="android phone"
>>> # Here we define a varible of String type
>>> type(a)
class str=""
>>> ram=4.5
>>> # Here we define a varible of float type
>>> type(ram)
class float=""

3 - Operations on the variables

Operators are symbols that manipulate variables, that is perform operations, evaluate them, ... There are several types of operators  :  calculation operators, assignment operators, comparison operators, logical operators, ...

3 - 1 Calculation operators

Calculation operators allow to mathematically modify the value of a variable :

  • "+"  addition operator Adds two values
  • "-"  subtraction operator Subtracts two values
  • "*"  multiplication operator Multiplies two values
  • "/ "  division operator Divides two values
  • "="  assignment operator Assigns a value to a variable :  x = 3 Put the value 3 in the variable x.

3 - 2  Assignment Operators

These operators simplify operations such as adding a value to a variable and storing the result in the variable. For example: x = x + 2
With the assignment operators it is possible to write this operation in the following form: x + = 2.
That is, if x = 3 it becomes after the operation 5.

3 - 3 Concatenation of variables

The concatenation of two strings variables is the operation of putting one of the strings followed by the other and storing the result in another variable

Example 

>>> d="Desk"
>>> t="top"
>>> dt=d+t
>>> # Here dt is the concatenation of "Desk" and "top" and take as value "Desktop"
>>> dt
'Desktop'

Note : You can note concatenate a string variable with a numerical variable. But you can do it after  transformation from the numeric type to the string type with the str() method.

Example

>>> ram=5.6
>>> print("The ram value is equal to : " + str(ram))
The ram value is equal to : 5.6
Younes Derfoufi CRMEF OUJDA

Leave a Reply