fusebulbs.com
Building blocks of Python language
pythoncodingwithanish

Tokens and character sets are Building blocks of the Python programming language.

Character set

All characters that python can recognize. The below table illustrates the Python character set along with examples.

character Set Examples
Letters:-Upper case and lower case english alphabets A-Z,a-z
Digits:-all digits 0-9
Special symbols space,+,-,**,*,%,//,/,==,!=,>,<
Whitespaces Blank space,tabs
Other unicode characters All ASCII and Unicode characters

What are tokens?

Tokens in python are building blocks of the Python programming language.

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

Python has the following tokens:-

1>keywords

2>identifiers

3>literals

a>String literals

b>Numeric literals

c>Boolean Literals

d>Special literal None

TokensExamples
Keywords:-Words that are already defined and convey a special meaning to the language compiler/interpreterFalse,True,if,elif,else,for,
while,pass,continue,lambda,
return,finally,import,def
Identifiers:-names given to different parts of program like variables,functions,object,class,names given to different datatypes.def square,num=20,a_lst=[1,2,3];here square,num and a_lst are identifiers.
Literals/Constants:-Data items that have fixed valuesString:-‘Mayank‘,’abc‘,’anish‘;Numeric:-1,1.2,4,-3.95;Boolean:-True,False
Special literalNone;meaning nothing

1>Keywords

Keywords are reserved words having a particular purpose. For example:-True, False, for, elif, while, etc.
The purpose of the keyword is already defined. You cannot change it. When you name any variable as a keyword and assign some value, 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, classes, 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 call 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 begin 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 with underscores. It should not be a list, str, or any keyword.

Dynamic Typing?

In Python, you don’t have to declare the variable type while assigning a value to a variable.
Python states the kind of variable in the runtime of the program.
So, Python is a dynamically typed language.
myNum=20
myNum=’Mayank’
myNum=[1,2,3,4]
There is no declaration of type before assigning value to a variable. So, Python is a dynamically typed language.
In other programming languages like Java, C, C++, you have to declare the variable type before using it.
For example, you have declared myNum as an int type in Java before using it, so it is statically typed.
int myNum;
myNum = 15;
myNum = ‘Mayank;–>this will throw error

###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 an integer, it will throw an error.

Python is dynamically typed, i.e., You can set a different data type to the same variable.

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 variables to different data types.

3>literals

Literals are constant values that do not change. For example “John”,2.5,3,True. In python, we have String, number, boolean, etc as literals.

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.