Skip to main content

Python's Token

Tokens are the smallest individual unit in a program. It's also called lexical unit.

Types of token

  • Identifiers
  • Keywords
  • Operators
  • Literals
  • Punctuators


Identifiers

Identifier are used as a name to identify variables, list, dictionaries, classes, objects, variables, functions, etc.
Some rules of making identifier:
• It can be of any length.
• The first character should be a letter or underscore ( _ ).
• Keywords cannot be used as an identifier.
• Digits can be used except in the first character of an identifier.
• No special characters should be used except underscore ( _ ).
For example:

_hello
_Hello
Python1

Keywords

Keywords are reserved words in a programming language which has a special meaning.

Operators

Operators are the characters that triggers some computation when applied to variables.

There are various types of operators in Python:
  • Arithmetic Operators
  • Relational Operators
  • Membership Operators
  • Bitwise Operators
  • Identity Operators
  • Logical Operators
  • Assignment Operators

Literals

Literals are data items that have a fixed value.

Types of literals are as follows:
  • String Literal
  • Numeric Literal
  • Boolean Literal
  • None Literal (Special Literal)

Punctuators

Punctuators are symbols to organise sentence structure, expression, statements and program structure in a programming language.

Example:- ' " () {} [] \ # @ , ` : = .

Skeleton of Python Program

Expression
Expression is any combination of characters and symbols that represent a value.
For example:
a+7, a*5+b+3, etc.

Statement
Statement are used to take some actions or define instructions or functions.
For example:
print ("Hello")
This statement will call print() function.

Function
Function is a group of codes that has its name and it can be executed again by specifying its name.You will get to know more about functions in further chapters.

Blocks
Blocks are the group of statements that is a part of another statement or functions.
For example:

Indentation are used to specify blocks. You cannot use indentation unnecessarily, it will raise an error.

Comments
Comments are just an additional information source, which is read by the programmers, but ignored in execution process. In python, # symbol is used to specify comments.

Dynamic Typing

In python, a variable that points to a value of one type can be use again to points to another value.

For example:

Input Code :

a=5
print(a)
a="hello"
print(a)
Output Code :
5
hello

Comments

Post a Comment

Popular posts from this blog

Introduction to Python

Python is a high level, object oriented programming language. It has simple and easy to learn syntax. It is good for beginners as well as for developers. Python is used in various fields now with its growth in popularity as compared to other Programming languages like Java, C, PHP, etc. Python was developed by Guido Van Rossum in February 1991 . Python was supposed to be a successor of the ABC language. Python programming language was named after the famous BBC Comedy show, Monty Python's Flying Circus.

Programming Questions

Programming Questions with source code Program 1: Program to obtain side of a square and then calculate its area.   Input code:  a=int(input("Enter the side of square:")) sq=a**2 print("Area of square is :",sq) Program 2: Program to obtain length and breadth of a rectangle and calculate its area. Input Code:   l=int(input("Enter length of the rectangle:")) b=int(input ("Enter breadth of the rectangle:")) area=l*b print("Area of rectangle is:",area) Program 3: Program to obtain height and base of a triangle and calculate its area.   Input Code: l=int(input("Enter length of triangle")) b=int(input("Enter base of triangle")) area=0.5*b*h print("Area of triangle is:",area) Program 4: Program to obtain a number and print its square and cube. Input Code:   num=float(input("Enter the number:")) sq=num*num cube=num*num*num print("Square of",num,"is",sq) print("Cube of",num,&q

Variables & Type of Literals and Operators

Variables in Python Variables are the character(s) used to store a value to be used further in the program. For example, when you want to store roll number of a student in the program then we use some variables to store roll number data:                      Roll_no = 35 In the above statement we use an identifier(Roll_no) to store roll number of a student of integer type. Then we can call Roll_no a numeric variable.                      Em_name = “Sanu” Then we can call Em_name a string variable.   IMPORTANT: In Python, you cannot use integer, float, complex number, expression as a variable as it will raise an error. You can only use characters, or identifiers to store any value. For example: 36=Id 25=x 2a/c=20     In Python, you can use multiple assignments.  #that’s nice For example: A,B,C=5,10,15 x=y=z=50   Dynamic Typing A variable who is used to store a value of one type, can be used again to store a value of different type. Python uses D