.feed-links {display:none !important;} -->

Python calculations and variables

 

Calculations

One of the first functions of an interpreter is to do calculations:

> > >  1 + 2 
3

You can add spaces, it will not affect:

>>>  1  +  2 
3

All operators can be used:

>>>  1 - 10 
- 9 
>>>  2 * 10 
20 
>>>  100 / 4 
25 
>>>  10 % 4 
2 
>>>  2 ** 3 
8

The double star represents the exponent.

There is, however, one mistake to avoid for Python versions lower than Python 3 :

>>>  10 / 3 
3

Suprise 10/3 = 3. So what is this madness? Python reasons in whole numbers since we have given him two integers. To get a result in decimals, you will need to use this syntax:

>>>  10.0 / 3 
3.3333333333333335 
>>>  10 / 3.0 
3.3333333333333335

The variables

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 which indicates the location of the RAM where the information that we have linked with is stored.

Let's assign a value to the age variable that we'll then display:

>>>  age  =  30 
>>>  age 
30
We will then add 10 to the value of this variable:
>>>  age  =  30 
>>>  age  =  age  +  10 
>>>  age 
40
It is possible to put a variable in another variable.
>>>  age  =  30 
>>>  age2  =  age 
>>>  age2 
30

You can put pretty much anything you want in your variable, including some text:

>>>  age  =  "I am 30 years old" 
>>>  age 
"I am 30 years old"

It is possible to concatenate, i.e. to add text to text:

>>>  age  =  age  +  "and I'm still young!" 
>>>  age 
"I am 30 years old and I am still young!"

You can even multiply a string of characters:

>>>  age  =  "young" 
>>>  age  *  3 
'jeunejeunejeune'

Obviously, if you try to add some variables that are numbers and others that are text, the interpreter will scold you:

>>>  age  =  "I am 30 years old" 
>>>  age 
"I am 30 years old" 
>>>  age  +  1 
Traceback  ( most  recent  call  last ): 
  File  "<stdin>" ,  line  1 ,  in  < module > 
TypeError :  cannot  concatenate  'str'  and  'int'  objects

You notice that the interpreter is nice since it tells you what is wrong: It cannot concatenate str and int .

Escaping quotes

How to include the quote character since it indicates a start of data and end of data?

>>>  text  =  "Hello my name \" Olivier \ " " 
>>>   text 
Hello  I  m is called "Olivier"

or

> > > Text =  'Hello I \' m called "Olivier" ' 
> > > text
 ' Hello I \ 'm called "Olivier"'

There is another way to store text in a variable: using a triple quote. This makes it possible not to escape the quotes characters of the data.

>>>  text  =  "" " 
... Hello my name is" olive tree " 
..." "" 
>>>  text 
' \ n Hello I m \ called "olive" \ n "

Naming a variable

You cannot name the variables however you like, since there are already words used by Python. Here is the list of words reserved by python:

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

Why are these words reserved? Because they are used to do something else. We will see what in more detail in the next chapters.

To name a variable, you must use the letters of the alphabet, numbers and the character "_" and "-". Do not use accents, punctuation marks, or the @ sign. In addition, the digits must never be in the first position in your variable:

>>>  1 var  =  1 
  File  "<stdin>" ,  line  1 
    1 var  =  1 
       ^ 
SyntaxError :  invalid  syntax

As you notice, python refuses this kind of syntax, but it will accept var1 = 1 .

Types of variables

In python a variable is typed , that is to say that in addition to a value, a variable has a kind of label which indicates what this virtual box contains.

Here is a list of variable type:

  • The integer or whole numbers : as the name suggests a whole is a number without decimals.
  • The float or comma number : example: 1.5
  • The strings or character string : to keep things simple that is not a number.

There are plenty of others, but it may still be a little too early to tell you about them.

To know the type of a variable, you can use the " type () " function
>>>  v  =  15 
>>>  type ( v ) 
< type  'int' >
>>>  v  =  "Olive 
tree  " >>> type ( v ) 
< type  'str' >
>>>  v  =  3.2 
>>>  type ( v ) 
< type  'float' >

The Decimal lib for calculations

The result of calculations in python may be different depending on the machines you use if you work with more precision using Decimal.

from  decimal  import  Decimal 
>>>  a  =  10 
>>>  b  =  3 
>>>  a / b 
3.3333333333333335 
>>>  a  =  Decimal ( '10' ) 
>>>  b  =  Decimal ( '3' ) 
>>>  a / b 
Decimal ( '3.333333333333333333333333333' )

No comments:

Post a Comment