Class 10 AI Chapter - Python Topic - Tokens - Arvindzeclass - NCERT Solutions

Post Top Ad

Sunday, July 13, 2025

Class 10 AI Chapter - Python Topic - Tokens

 Python Character Set

The character set in Python includes letters, digits, whitespace, special symbols, and Unicode characters. It allows writing code using English alphabets, numbers, operators, and punctuation marks.


1. Letters

Python supports both uppercase (A–Z) and lowercase (a–z) letters. These are used in identifiers like variable and function names.

Example:

Name = "Alvin"
language = "Python"

2. Digits

Python uses digits 0 to 9 in numeric literals and identifiers.

Example:

age = 25
roll_no1 = 101

3. Special Symbols

Python uses various symbols to define expressions, statements, and blocks.

Examples:

+ - * / = == != < > ( ) [ ] { } : , . #

Used in arithmetic and logical operations:

a = 10
if a > 5:
    print("Greater")

4. Whitespace Characters

Spaces, tabs (\t), and newlines (\n) are used to separate code elements and format blocks (like indentation).

Example:

if True:
    print("Indented block")

5. Unicode Characters

Python supports Unicode, allowing use of characters from all languages and even emojis.

Example:

greet = "नमस्ते"
smile = "😊"


Statements in Python

A statement in programming is a single instruction that performs a specific action, such as assigning a value, printing output, or making a decision. It is the smallest executable part of a program. Statements are executed in sequence unless control flow alters their order using conditions or loops.


1. Simple Statement

A simple statement is a single line of code that performs a specific action.

Example:

x = 10
print("Hello, World!")

2. Multiple Statement

Multiple statements are written on a single line, separated by semicolons (;). This is not recommended for readability but is allowed.

Example:

a = 5; b = 10; print(a + b)

3. Conditional Statement

Conditional statements are used for decision-making. They execute code based on certain conditions using if, elif, and else.

Example:

x = 15
if x > 0:
    print("Positive")
elif x == 0:
    print("Zero")
else:
    print("Negative")

These types of statements help control the flow and logic of a Python program efficiently.

Comments in Python

Comments are used to explain code and make it easier to understand. They are ignored by the Python interpreter and do not affect program output.

1. Single-Line Comment

A single-line comment starts with the hash symbol #. It is used to write short notes or explanations.

Example:

# This is a single-line comment
x = 5  # Assigning value to x

2. Multi-Line Comment

Python doesn't have a true multi-line comment, but you can use multiple # symbols or triple quotes (''' or """) for block-style comments.

Example using multiple #:

# This is a multi-line comment
# explaining what the code does
# line by line.

Example using triple quotes (commonly for docstrings):

"""
This is a multi-line comment
using triple quotes.
Useful for large descriptions.
"""

Note: Triple-quoted comments are treated as strings, so they're only ignored if not assigned to a variable.

Token in Python

A token is the smallest unit of a Python program. Python breaks each line of code into meaningful pieces called tokens. These include keywords, identifiers, literals, operators, and punctuators.

1. Keywords

These are reserved words in Python that have special meaning. They cannot be used as variable names.

Examples:

if, else, while, for, def, return, True, False, None

Code Example:

if age > 18:
    print("Adult")

2. Identifiers

These are the names used for variables, functions, classes, etc.

Example:

name = "Alvin"
def greet():
    print("Hello")

3. Literals

These are constant values assigned to variables. Types include:

  • String: "hello"
  • Integer: 10
  • Float: 3.14
  • Boolean: True / False

Example:

x = 100
name = "Lily"

4. Operators

Used to perform operations on values or variables.

Examples: +, -, *, /, ==, >, <

Code Example:

a = 5 + 3

5. Punctuators (Separators)

Symbols that help structure the code.

Examples: (), :, ,, {}, []

Code Example:

def add(a, b):
    return a + b

These tokens are combined to form valid Python programs. 

Operator in Python

An operator is a special symbol that performs operations on variables and values. Python supports different types of operators for arithmetic, comparison, logic, assignment, and more.

1. Arithmetic Operators

Used to perform basic mathematical operations.

Operator Description Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
// Floor Division a // b
% Modulus a % b
** Exponentiation a ** b

Example:

a = 10
b = 3
print(a + b)   # 13
print(a ** b)  # 1000

2. Assignment Operators

Used to assign values to variables.

Operator Meaning
= Assign
+= Add and assign
-= Subtract and assign
*= Multiply and assign

Example:

x = 5
x += 3  # Same as x = x + 3
print(x)  # 8

3. Comparison Operators

Used to compare two values.

Operator Meaning Example
== Equal to a == b
!= Not equal a != b
> Greater than a > b
< Less than a < b
>= Greater or equal a >= b
<= Less or equal a <= b

Example:

a = 10
b = 5
print(a > b)  # True

4. Logical Operators

Used to combine conditional statements.

Operator Description
and True if both are true
or True if at least one is true
not Inverts the result

Example:

x = 5
print(x > 3 and x < 10)  # True

5. Bitwise Operators

Operate on bits directly.

Operator Description
& AND
` `
^ XOR
~ NOT
<< Left shift
>> Right shift

Example:

a = 5      # 0101
b = 3      # 0011
print(a & b)  # 1 (0001)



No comments:

Post a Comment

Post Top Ad