2. Variables in Python

A variable is a unit of data with an identifier which is held in the computer’s memory. It can be changed by putting a new value into it or modifying the value that is already there.

Here you will be introduced to some different types of variables that are available for us in our programs. We will have a brief look at how to build them into expressions and statements of Python that will allow you to turn your designs into working code.

Good Identifiers:

Identifiers are names used to identify things in our code. Any word which is not commented out (by the use of // OR */  )  or  escaped in a certain way (one of the escape char is ) is identified as a Variable in Python. An identifier cannot be a keyword ( words predefined in Python 3 assigned for a specific task).

Following are a few predefined keywords in Python3.

  • False
  • True
  • global
  • continue
  • yield
  • with
  • nonlocal
  • pass
  • return
  • raise
  • try
  • import
  • assert
  • break

These identifiers will result in Syntax error.Untitled

 

Naming Rules:

So lets talk about what is permitted as a declaration for variables.

Variable letter must begin with a letter or an underscore. They can contain numbers, but they should not start with a number. This is because; if the interpreter encounters a bunch of characters starting with a  number, it will assume it as a number and classify it as a valid number class if it fits in the syntax.

Python is a “case-sensitive” language. Thus lower case and upper case are treated as different characters. Therefore Slidenerd and slidenerd are two different variable names or identifiers.

Creating Different Variables and Assigning Values:

In any programming language, crating variables involve two steps. 1st is to create a box and stick an identification label on it; this is called as “Initialization”. 2nd is to put values into the box; this is called as “Assign”.  Initialization and Assign is done by using an “=” operator.

 Recognizing Different Types of Variables:

Types of variables depend upon the data type the store.

To identify the type of variable, we write

type(Variable_name)

 

Assigning Expressions:

In python shell we can calculate simple arithmetic expressions

variables can be declared as a camel cast, i.e , “lostBag”, as variables cannot have a space between two words, Camel Casting is used in such case.

The above snip-it demonstrated simple arithmetic expression. The python shell is advanced enough to simply and calculate.

Congratulations!! you have now learned about how to define variables and assign expressions.

Next: we will learn about Data Types