×

Python tokens and character set

In this tutorial, we will understand what are character sets used in python and what is meant by python tokens and we will further discuss the type of tokens being used in python.

As we know, Python is a general-purpose, high-level programming language. It was developed to ease the developers by enabling them to achieve their tasks by writing fewer lines of code, and these codes are known as scripts. These scripts consist of character sets, tokens, and identifiers. In this tutorial, we will focus on character sets and tokens.

Character set

A valid set of characters that is suitable for a programming language in scripting is known as the character set. Here, we are referring to the Python programming language. Thus, the Python character set is a valid set of characters identified by the Python language. We can use these characters while writing a script in Python. Following are the ASCII / Unicode characters that are supported in the python programming language:

  • Alphabets: It includesall the upper case (A-Z) and lower case (a-z) alphabets.
  • Digits: It includesall the digits ranging from 0-9.
  • Special Symbols: It includesall kinds of special symbols like, ” ‘ l; : ! ~ @ # $ % ^ ` & * ( ) _ + – = { } [ ] \ .
  • White Spaces: It includes the non-printable characters known aswhite spaces such as tab space, blank space, newline, and carriage return.
  • Other: Python supportsall ASCII and UNICODE characters.

Tokens

Now, let’s understand tokens in python.

The smallest individual unit in a python program is termed a token. Tokens cannot be further divided into subunits. With tokens, we can build all the statements and instructions in a program. There are several tokens used in python. Let’s discuss each one below:

1. Keywords

 Keywords are those reserved words that have got some unique meaning or importance in a programming language. One has to keep the restraints on these keywords while writing the code. Keywords can’t be used as the name of the variable, the name of the function, or for any other random purpose. They are reserved for special usage only. The numbers of keywords are 33 in Python. Some of them include: Elif, from, try, False, True, class, break, continue, and, as, assert, while, for, in, raise, except, or, not, if, print, import, etc.

Now, let us understand this through an example.

Example:

# python program to understand keywords used in python
for i in range(1, 20):
	
	# Printing the value of i
	print(i)
	
	# Checking whether the value of i is more than 10 or not
	# if the value of i comes out to be more than 10 
	# then the loop will continue
	# Here, if, continue, else, break,
	# for loop are keywords
	if i > 10:
		continue
	
	# if i is less than 10 then break the loop
	else:
	break

Output:

Python tokens and character set

2. Identifiers: Identifiers are another keyword supported in python language. These are the names provided to any variable, function, class, list, methods, etc. for their recognition. As Python is sensitive toward the case(case-sensitive), certain rules are to be kept in mind before naming any identifier.

Some of the rules are listed below to name an identifier: -

As specified above, Python is a case-sensitive language, Thus, it treats the same word differently. For example – cat and CAT are two different words for python. Thus, the case is an important aspect of naming identifiers.

  1. Identifiers can begin with an upper case character (A-Z) or a lower case character (a-z). Even an underscore( _ ) is also permissible here.

    Apart from these, It can’t begin with other characters.
  2. Digits can also be a part of an identifier but can’t be the first character. Besides the upper case, lower case characters, and underscores. An identifier can also consist of a digit, but can’t begin with a digit.
  3. An identifier doesn’t permit any other special characters or whitespaces to be part of an identifier.
  4. As keywords are reserved for specific tasks, they can’t be a part of an identifier.

Now, let us understand this through an example.

Example:

# Here, tutorials and examples are the valid identifiers


tutorials = 'Hello world'
examples = "Bonjour"


# Driver code 
print(tutorials)
print(examples)

Output:

Python tokens and character set

3. Literals or Values: The static values or data items that are written in a source code are termed literals. Various types of literal are supported in python.

The Following are the types of literals:

  • String Literals: The string literals are those literals that are written as text within quotes - single, double, or triple. For example: “Computer Science and Engineering”, ‘India is a secular country’, etc. The triple quotes can be taken into account to write multi-line strings.
  •  Character Literals: Character literal is another type of literal used in python. Here, instead of a string, characters are enclosed within a single or double- quotes.
  • Numeric Literals: These are other types of literals used in python. They are written in the form of numbers. The following numerical literals are supported in python:
  • Integer Literal: It includes both positive and negative numbers along with 0. It doesn’t include fractional parts. It can also include binary, decimal, octal, hexadecimal literal.
  • Float Literal: It includes both positive and negative real numbers. It also includes fractional parts.
  • Complex Literal: It includes a+bi numeral; here a represents the real part and b represents the complex part.

Related Topics

Python Interface

When creating an application, it is important to continuously keep track of its changes. As an application grows, sometimes it gets hard to manage its updates and changes. Often, you...

4 minutes read.

How to Iterate a List in Python

As we know, a List is one of the four unique data structures available in Python; in this article, we will deep dive into understanding iterating through a list and...

3 minutes read.

os.rename() method in Python

In Python, the os module provides the capacity to interact with the operating system. The operating system comes under the Python module. In this module, Python provides a specific feature...

3 minutes read.

Python String isupper() method

Python String isupper() method The String.isupper() method in Python returns a boolean value ‘true’ if all cased characters in the string are uppercase else for any other value is returns false. Syntax String.isupper() Parameter NA Return This...

2 minutes read.

Python Comment Block

In this tutorial, we will see what comment blocks mean in Python. Further, we will see the commenting methods supported in Python. We will understand the topics deeply with the...

6 minutes read.

Python Kwargs Example

In this article, we'll talk about Python's kwargs notion. In Python, kwargs has two stars and passes a variable number of keyworded argument lists to the function, whereas args has...

5 minutes read.

STL in Python

Python has many libraries that are very useful and simplify many complex codes. In the same way, STL is also one of the python libraries used for reading and writing...

3 minutes read.

Python Recursion

Recursion is one of the most interesting yet important concepts of any programming language. If you want to be a good programmer or data scientist, then you should better have...

4 minutes read.

What does base case mean in recursion

Base cases and recursive stages are used to define recursive functions. In a primary subject, we compute the outcome right away, given the function call's arguments. In a recursive step, we...

4 minutes read.

Python String rsplit() method

Python String rsplit() method The string.rsplit() method in Python splits a string into a list, starting from the right. If the "max" parameter is not specified, this method will return the...

1 minute read.

What is Python 2

Python is a widely used high-level language. The initial work on developing python was begun in the late 1980s. In 1989, Guido Van Rossum started to work on it. Initially,...

3 minutes read.

Python Program for Tower of Hanoi

In this tutorial, we will examine and learn about the Python coding used to create the video game Tower of Hanoi. Before seeing the game's step-by-step implementation in Python, we...

3 minutes read.

Dictionary to JSON Python

In python JSON (JavascriptObject Notation). In the programming language, the text file is made using the script file.We can use many built-in packages which arenamedJSON.Before using the packages, we have...

3 minutes read.

List Assignment Index out of Range in Python

As we know, a List is one of the four unique data structures available in Python; In this tutorial, we will deep dive into understanding iterating through a list and...

3 minutes read.

Composition in Python

Composition in Python Composition is one of the important concepts of Object-oriented programming (OOPs). Composition basically enables us for creating complex types objects by combining other types of objects in the...

6 minutes read.

Drop() Function in Python

Drop() Function: The python programming language consists of various libraries; some of them are pandas and matplotlib. Data scientists mainly use the pandas library to analyze the data more easily and...

3 minutes read.

GUI Calendar using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

3 minutes read.

Python datetime

Python provides a module named datetime to work with the date and time. Sometimes in the real life application development, we need to work with the date and time. The date is...

3 minutes read.

Best Database in Python

In this tutorial, Firstly, we will understand what the term database means. Further, we will see the various databases supported in Python and when to use which one. Further, we...

7 minutes read.

Python Skyline

Python Programming Language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.