Python Tokens

Python Tokens are individual units in a program. The following are the types of Tokens:

  1. Keywords
  2. Identifiers
  3. Literals

Keywords

Keywords are reserved words used in programming languages. The compiler/ interpreter already knows these names, so you cannot use them as variable names.

List of Keywords in Python

Keywords in Python

Identifiers in Python

Identifiers in Python are used for naming variables, functions, and arrays. Do not use keywords as identifiers.

An identifier begins with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores, and digits (0 to 9). That means, it is a combination of character digits and underscores.

Python Identifiers

Literals

Literals are the values assigned to each constant variable. Python has the following literals:

  • String Literals: Enclose the text in quotes. Can use both single and double quotes. Use triple quotes for multi-line strings.
  • Numeric Literals: Includes int, long, float, and complex.
  • Boolean Literals: Can have either True or False values

Let us now see examples of Literals in Python:

String Literals in Python

Demo5.py

# String literals in Python
# Code by studyopedia

str = "Amit"
print(str)

str2 = 'John'
print(str2)

str3 = """ Hi,
How are you
"""
print(str3)

The output is as follows:

Amit
John
 Hi,
How are you

Numeric Literals in Python

Demo6.py

# Numeric Literals in Python
# Code by studyopedia

# int Literal
val1 = 10
print(val1)

# float Literal
val2 = 20.60
print(val2)

# complex Literal
val3 = 2+5.6j
print(val3)

# hexadecimal Literal
val4 = 0x11d
print(val4)

# octal literal
val5 = 0o023
print(val5)

The output is as follows:

10
20.6
(2+5.6j)
285
19

Boolean Literals in Python

Demo7.py

# Boolean Literals in Python
# Code by studyopedia

val1 = (1 == True)
val2 = (1 == False)

val3 = val1 + 5
val4 = val2 + 5

val5 = True + 5
val6 = False + 5

print(val1)
print(val2)
print(val3)
print(val4)
print(val5)
print(val6)

The output is as follows:

True
False
6
5
6
5

Python Tutorial (English)

If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

Python Sets With Examples
Python Comments
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment