Python Tokens
- Categories Python Tokens, Python, Python
Q. 1 What is Tokens in python?
Ans.
- A token is the smallest individual unit in a program.
- Like English language has noun, verbs, adjectives etc.
Q. 2 What are the different types of tokens?
Ans. Python has 5 types of tokens.
Q. 3 What are keywords in python?
Ans.
- Keywords are reserved words which have a special meaning to the compiler or interpreter so it cannot be used as identifiers.
- Python language has these as reserved keywords.
- You cannot use keywords as regular identifiers.
- Some which have caps False, True, None
and | del | for | is | raise |
assert | elif | from | lambda | return |
break | else | global | not | try |
class | except | if | or | while |
continue | exec | import | pass | with (2.5) |
Q. 4 What are the rules for naming a identifier?
Ans.
- An identifier starts 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).
- Case is significant in Python: lowercase and uppercase letters are distinct.
- Python does not allow punctuation characters such as @, $, and % within identifiers.
Q. 5 What are identifiers in python and what are the rules for naming an identifier?
Ans.
Python identifiers which is a name we give to any variable, function, class, object etc.
The rules for naming an identifier:
- An identifier starts 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).
- Case is significant in Python: lowercase and uppercase letters are distinct.
- Python does not allow punctuation characters such as @, $, and % within identifiers.
Q. 6 What are Literals?
Ans.
- Literals can be defined as number, text, or other data that is to be stored in variables.
- It can be of different types like String, Boolean, Numeric
- Example:
- 33 # Integer literal
- 14 # Floating-point Fractional literal
- ‘hello’ # String literal
- “world” # Another string literal
- True / False # Boolean Literal
Q. 7 What are operators in Python? What are different types of operators in Python?
Ans.
- Operators which are special symbols that perform specific operations on Operands.
- Operators fall into seven different categories: assignment, arithmetic, relational, logical, bitwise, membership and identity.
- Python has incorporated operators like +=, but ++ (or –) do not work in Python
Q. 8 Operator Precedence Table
Ans.Operators Meaning ( ) Parentheses ** Exponent +x, -x, ~x Unary plus, Unary minus, Bitwise NOT *, /, //, % Multiplication, Division, Floor division, Modulus +, – Addition, Subtraction <<, >> Bitwise shift operators & Bitwise AND ^ Bitwise XOR | Bitwise OR !ERROR! A11 -> Formula Error: Unexpected operator ‘=’ Comparisions, Identity, Membership operators Not Logical NOT And Logical AND Or Logical OR
Q. 9 What is the difference between is and in operator.
Ans.
is and is not are the identity operators in Python. They are used to check if two values (or variables) are located on the same part of the memory. Two variables that are equal does not imply that they are identical or located at same memory location.
in and not in are the membership operators in Python. They are used to test whether a value or variable is found in a sequence (string, list, tuple, set and dictionary).
In a dictionary we can only test for presence of key, not the value.
Q. 10 How are the following two expressions different?
- ans = 9
- ans == 9
Ans. One is using assignment operator and other is using comparison operator.
Q. 11 Write one difference between / and // operator.
Ans. / is the floating point division operator which divides and gives floating value.
// is the integer division operator which discards the decimal value after decimal point and gives only the quotient.
e.g. next question
Q. 12 Evaluate the following if a = 15 , b = 3
- a / b
- a // b
Ans.
- 3
- 5
Q. 13 Which of these is invalid identifier?
- $age
- 1percent
- _
- A
- and
- While
Ans. a, b,e are invalid. Remaining are valid
Q. 14 Which of the following are valid comments?
- /* comment */
- */comment*/
- ‘’’ comment
- //comment
Ans. Comments a and d is valid.
Q. 15 Identify which Python token are these?
- and
- _value
- True
- <=
Ans.
- Keyword
- Identifiers
- Boolean literals
- Relational Operator
Q. 16 Identify which of the following as valid variable names, state reason if invalid.
- _ 100
- 1
- break
- A463
- T 204
Ans.
- Valid as it begins with (underscore).
- Invalid, dot (.) is not allowed.
- Invalid, as break is a keyword.
- Valid
- Invalid, space is not allowed.
Q. 17 Determine the type of literals below.
- -19
- 0o12
- 0XFACE
Ans.
- Decimal integer literals
- Octal Integer Literals
- Hexadecimal Integer Literals
Q. 18 If i = 3, j = 2 what is the result of following expressions?
- i + 5 >= j – 6
- j * 10 < i ** 2
- i < j + 5 > j ** 4
Ans.
- True
- False
- False
Q. 19 Identify errors in following statements and correct them:
- number = 5,000
- byte x= = 3
- x/10 = y
- number = +20
Ans. correct statement:
- number = 5000
- byte = 3
- y= x/10
- number += 20
Q. 20 Identify the following literals:
- 23
- True
- ‘b’
- “a”
Ans.
- Floating point literal
- Boolean literal
- Character literal
- String literal
Q. 21 Identify the Valid and invalid Operators?
- <>
- <=
- //=
- &
Ans.
- Invalid
- Valid
- Valid
- Valid