×

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 program to sort the array element into ascending order

Python program to sort the array element into ascending order In this example, we'll see how to sort the array elements in ascending order using a Python program. Sorting is a...

2 minutes read.

Python md5() function

Python md5() function The md5() function in PHP calculates the md5 hash of a string. Syntax md5 ( string $str [, bool $raw_output] ) Parameter str(required)- This parameter specifies the string. raw_output(optional)- This parameter specifies a hex or binary output format: TRUE - Raw 16 character binary...

1 minute read.

Writing to a CSV file in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

6 minutes read.

Reverse a String in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

4 minutes read.

Bar Plot in Python

A bar chart or bar graph displays categorical data using rectangular bars with heights or lengths proportionate to the values for the data distributed in the dataset. In a bar...

16 minutes read.

Method Overloading in Python

Overloading is a capability of a method and operator to behave differently according to the nature of parameters. Overloading is popular because it provides a good number of advantages: Reusability of code.Reduces...

3 minutes read.

How to Plot Graphs Using Python?

In this article, we are going to learn about how to plot different types of graphs using Python. We are going to use different approaches for every type of graph....

3 minutes read.

How to run Python program in CMD

How to run python program in cmd Command Prompt provides us all together with a different approach for dealing with our programs. The programs can be executed by accessing the directories. In...

3 minutes read.

How to find square root in python

How to find Square Root of a number In Python Python makes a lot of tasks easier by using different functions, modules, and libraires. There is an inbuilt function in Python...

6 minutes read.

Accessing Key-value in Dictionary in Python

A Python word reference is an assortment of key-esteem matches where each key is related to worth. Worth in the key-esteem pair can be a number, a string, a rundown,...

5 minutes read.

Python List clear() method

Python List clear() method The list.clear () method in Python extends the list by appending all the items from the iterable. Syntax list.clear() Parameter NA Example 1 # Python Program explaining # the list.clear() method week_list = ["Monday", "Tuesday", "Wednesday","Thursday","Friday",...

1 minute read.

Check if the directory exists in Python

To prevent any error, while loading or editing a file, we first have to check that the directory or the file exists or not. This is also done to prevent...

5 minutes read.

Python Lexicographic Order

Python lexicographic order Before we discuss the lexicographic order in Python, we should understandwhat is lexicographic order and sort according to lexicographic order. Lexicographic order In mathematics, the generalization of the alphabetical order...

5 minutes read.

SKLearn Model Selection

The model selections of SK learn has many functions with which we can work on.It has functions to cross-validate the model and, it also provides validation and learning curves.It is...

3 minutes read.

Google Python Class

Python: 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 faster way....

3 minutes read.

Python Keywords

If you are trying to learn a programming language, you need to have a basic idea of "What are keywords" and "How they are used". You can learn about keywords in...

7 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.

Sort Dictionary in Python

Dictionaries In Python, a dictionary is an unordered collection or set of data types that enable of store data in an unordered key-value/pair. The key is stored alongside with value. Dictionary contains key:...

4 minutes read.

XXhash Python

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

6 minutes read.

Python String Variable

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...

4 minutes read.