Revision Of Python Basics - Python Tokens

 


In this post we are going to study what is Python Tokens, Use of Python Tokents, Different type of Python Tokens, Etc.

Tokens are the smallest units of a Python program. Each Python program is made of small elements called tokens. There are different types of tokens such as identifiers, operators, literals, etc., that make up statements of Python programs. Tokens are separated by white spaces. A token can be a single character or a combination of characters. Let us learn the concept of tokens with the help of a given program.

In the program, # sign marks a comment, hence '# Program to add two numbers ' is a comment statement, x, y, z are known as variable. = and + sign are the operators and 'print' is a keyword.

Thus, different tokens used in Python program are as follows.

Tokens

Literals

Keywords

Identifiers

Punctuators

Variables

Operators

Let us learn about these tokens.

Literals: are also referred to as constants as these are the fixed values that do not change during the execution of a program. These are inbuilt objects in Python. Different types of literals that are used in Python programs are as follows.

Literals

Numeric

Character

String

Boolean

Special

Collection

Numeric

Integer

Long

Float

Complex

Numeric Literals: are the fixed numeric values used in the Python programs. These are not changeable and are used as they are in the calculations.

Different types of numeric literals are:

Integer Literals: These are the whole numbers without fraction or decimal point such as 23, 46, 78, etc..

Long Literals: These are the integers with unlimited size. These are either in decimal form, in octal form or in decimal form. 

Floating Point Literals: These are the numeric values with decimal points such as 45.65, 34.23, 678.90, etc.

Complex Literals: These are the complex numbers such as 2 +3i, 3.3 + 2i, etc.

Character Literals: are any single character, typed using the keyboard. These are enclosed either in single or double quotes such as 'A', "#", "t", etc.

String Literals: are a combination of characters. These are enclosed either in single, double or triple quotes such as "Code With Anmol",""" I like learning Python. It is very interesting.""", 'Python', etc. A string literal enclosed in triple quotes is considered as multiple string.

Boolean Literals: contains any one of the boolean values, either true or false.

Special Literals: are the value none. They are used to define a field that is not created. 

Collection Literals: as the name suggests, collection literals are the collection of more than one literals. These are different literal collections such as:

1. List Collection [abc, def, ghi]

2. Tuple Collections (23,24,25)

3. Dictionary Collections {'a':'apple', 'b': 'ball', 'c':'cat'}

4. Set Collections {'a', 'b', 'c'}

Example:

Output:

Keywords

Just like all other languages, computer programming languages also have reserved words known as keywords. For example, in the English language, words like being, are, have, etc., have some predefined meaning. These words cannot be used to name an object such as a person, place or thing. In the same manner, the keywords in Python are the reserved words, those meanings are already defined in the language interpreter. These words cannot be used to name objects such as variables, classes, functions, etc.. These are the approximately 33 keywords I Python languages.

These are as follows.

False None True And As
assert break class continue def
del elif else except finally
for from global import if
in is lambda nonlcal not
or pass raise return try
while with yield --- ---

Identifies

The identifiers are the words or names given to different objects such as variables, functions, classes, etc.,. in Python. In the case of English languages, we can name persons, places or things on out own as per our own wish. In the same manner, identifiers are user-defined names given to different objects. We only need to follow the naming rules to name different objects.

Naming Conventions For Identifires

The rules to be kept in mind while defining an identifier are as follows.

1. Only a combination of letters, i.e, small letters (a to z) or capital letters (A to Z), numbers  (0 to 9) and special character underscore (_) is allowed while defining an identifier.

2. Identifiers should always start with a letter, It cannot start with a digit.

3. Space is not permitted in an identifiers.

4. Keywords can be used as identifiers.

5. No special characters other than underscore (_) are allowed.

Punctuators

The punctuators referred to as separators in some programming languages are like punctuation marks. These are used to separate tokens and organize statements in Python programming

.1. Quote: '(Single Quote), "(Double Quote)

2. () (Parenthesis), [] (Square Bracket), {} (Curly Bracket)

3. @ (at the rate)

4. , (Comma), : (Colon)

5. . (Dot)

6. = (Assignment)

7. / (Forward Slash), # (Hash Sign)

Fact - Python is a case-sensitive programming language that means the lowercase name and UPPERCASE name are differences in Python programs. 

Variables

A variable is a name given to a memory location where some values are stored. The values stored in variables can be of different types, i.e., integer, float, string, etc. A variable is like a container that holds the values which can be used throughout a program and it keeps on changing, too.

For Example, num = 10

Here, name is a variable that holds on stores a value 10.

Let, num = 10 + 10

Now, the value of num is 20.

Thus, variable hold the values that keep on changing during the execution of a program.

Operators

The operators are mathematical or other symbols that work on the operands and are used for calculation.

For example, a = 90 + 10

In this statement, 90 and 10 are operands and + is an additional operators, logical operators, assignment operators, etc., in Python. Let us learn about arithmetic operators, in this post.

Arithmetic Operators In Python

The arithmetic operators are used to perform numerical calculations in Python. The following table shows the basic arithmetic operators used in Python, along with examples.

Assume that the values for the variables x and y are 30 and 20, respectively. A programmer does not need to worry about these tasks as these can easily be handled using the inbuilt functions of Python.

Operators Description Example Result
+ (Addition) It adds values on either side of the operators. x + y 50
- (Subtraction) It subtracts right hand operand from the left hand operands. x - y 10
* (Multiplication) It multiplies values on either side of the operators. x * y 600
/ (Division) It divides lest hand operand by the right hand side. x / y 1.5
% (Modulus) It divides lest hand operand by the right hand operand and return the remainder. x % y 10

We will again meet again in another post. Keep Growing, Keep Learning And Stay Safe.

Post a Comment

Previous Post Next Post