17 Aug Python Tokens
Python Tokens are individual units in a program. The following are the types of Tokens:
- Keywords
- Identifiers
- 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
Identifiers in Python
Identifiers in Python are used for naming of 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 underscore.
Literals
Literals are the values assigned to each constant variable. Python has the following literals:
- String Literals: Enclose text in quotes. Can use both single as well as double quotes. Use triple-quotes for multi-line string.
- 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# 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:
1 2 3 4 5 6 |
Amit John Hi, How are you |
Numeric Literals in Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# 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:
1 2 3 4 5 6 7 |
10 20.6 (2+5.6j) 285 19 |
Boolean Literals in Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# 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:
1 2 3 4 5 6 7 8 |
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:
No Comments