Social Icons

Saturday, May 4, 2019

Python Variables

 

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 ! Phonephone, 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 variable 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 

No comments:

Post a Comment

Category Of Mobile Courses

Actualités (644) Adsense (1) Affiliation (1) Algebraic Topology (2) Algorithmic (1) all-news (30) Android (5) Android App (8) Android app without code (4) Android Apps (195) Android Development (4) Android download (2) Android OS (3) AngularJS (1) Automata theory and formal language (5) C programming (5) Category and Functor (8) CMS (3) Computer Glossary (18) Create Mobile App With Ionic Framework (2) CSS (2) CSS-Cascading-Style-Sheets (4) Developpement Java (13) Differential Geometry (1) Django-Python-Framework (8) dropshiping (26) Earn Money by Internet (4) Emplois (23) Framework php (2) Fraud (2) HTML (7) Java For Beginners (10) Javascript (12) Kotlin Programming Language (8) Kotlin For Mobile Android (1) Linux Download (2) Marketing (5) Mobile (3) Mobile Courses (4) Mobile Marketing (4) MoneyGram (1) News (721) Node.js (5) Open Source (1) Photoshop (1) Protect Computer (1) Python (35) Python BeautifulSoup (1) Python For Data Science (2) Python-Books (6) Python-DVD-Training (1) Python-Exercises (175) Python-Framework (1) Python-IDE (1) Python-Modules (1) Python-pdf (2) Python-pyQt (1) Référencement (2) Script PHP (2) Security (6) SEO (1) Snipping Tool: Faq (1) Social Networks (1) Source Code (1) Statistics With SPSS (2) Surveillance Software (1) Travail à domicile (6) Tutoriels php en vidéos (2) Tutoriels-MySql (6) tutoriels-php (19) Utilitaires (1) VPS (1) Web Hosting (1) Webcam (1) Webmarketing (11) Western Union (1) Windows 10 (1) Windows 7 (4) Windows 7 Faq (2) Windows 8 (1) Windows Accessories (1) Windows Download (8) Windows Drivers (1) Windows Fonts (1) Windows Power Shell (2) Windows Registry (2) Windows Security (18) Windows Software (2) Windows Spyware (2) Windows utilities (3) Windows Virus (2) Windows Vista (3) Windows Wireless (1) Windows xp (1) Wordpress (1)
 

Sample text

Sample Text

 
Blogger Templates