Python tutorial

Tokens are Building blocks of Python programming language.

What are tokens?

Tokens in python are building blocks of Python programming language.

The role letters and words play for the English language, Similar role token play for a python programming language.

Python has following tokens:-

1>keywords
2>identifiers
3>literals
a>String literals
b>Numeric literals
c>Boolean Literals
d>Special literal None

1>Keywords

Keywords are reserved words having a special purpose.For example:-True,False,for,elif .
Their purpose has already been defined, you cannot change it. When you name any variable as a keyword and try to assign some value to it, it will throw an error.

>>> False=10
  File "<stdin>", line 1
SyntaxError: can't assign to keyword

2>Identifier

Identifiers are Names given to variables, functions, class etc.

What is a variable?

Variables are containers for storing data values. Python has no command for declaring a variable. A variable is created the moment you first assign a value to it.

Think of variables as an address and data/value as your belongings. whenever you need your belongings you visit your address. Similarly, whenever in programming, you need your data/value you visit your variable.

for example

myName='Anish'
age=29

The variable myName contains the value Anish, whenever I need that value I will use the variable myName.

print(myName)
>>>Anish

conditions for naming variables

1> A variable name must start with a letter or the underscore character.
2> A variable name cannot start with a number.
3>A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).
4>Variable names are case-sensitive (age, Age, and AGE are three different variables).

_myName='Anish'
print(_myName)
>>>Anish
2myName='Anish'
File "<ipython-input-3-2be3ad01e93c>", line 1
    2myName='Anish'
          ^
SyntaxError: invalid syntax

Best practice for naming a variable


variables names should be in lower case.
variables names should not be list, str, or any keyword.

So, refrain from using such names as variable names.

Dynamic Typing?


In many languages, while declaring a variable you have to mention the type of that variable. While storing the value in that variable you
have to adhere to that type.


for example, in java, you declare a variable like

###here int is data type and myVariable is variable name.
int myVariable


In variable myVariable(declared above) you can store only an integer. So, java is Statically typed.

if in myVariable you assign anything other than integer it will throw an error.

Python is dynamically typed,i.e different data types can be assigned to the same python variables.
We do not have to mention data type in python variable declaration or assignment.

Variable dynamically takes type based on the value assigned to it.

For example

myname='anish'
myname=100
myname=100.20

print(myname)
>>>100.20

you are assigning different data types to the same variable myname, that is why python is dynamically typed .you can reassign variable to different data types.

3>literals

Literals are constant value which do not change.

Sumita Python revision tour
In [1]:
myname='Anish'

myage=29
In [4]:
print(myname)
Anish
In [5]:
print(myage)
29
In [8]:
False=10
  File "<ipython-input-8-a18697708789>", line 1
    False=10
            ^
SyntaxError: can't assign to keyword
In [9]:
2name='anish'
  File "<ipython-input-9-d0236c79ad3e>", line 1
    2name='anish'
        ^
SyntaxError: invalid syntax
In [10]:
_name='anish'
In [11]:
print(_name)
anish
In [ ]:
Literals

Literals are constant value which do not change.
In [12]:
#String literal
"Subham is a good guy"
Out[12]:
'Subham is a good guy'
In [13]:
#numeric literal
10
Out[13]:
10
In [14]:
#boolean literal
True
Out[14]:
True
In [15]:
#boolean literal
False
Out[15]:
False

Next:- Basic Data Types in Python

By ANISH SINGH

IT Analyst at Tata Consultancy Services. Follow @aniluvall

One thought on “Building blocks of Python language”

Leave a Reply

Your email address will not be published. Required fields are marked *