Class 11 Python: Basics of Python Programming
Understanding the structure and basic elements of a Python program is essential for writing clear and efficient code. Below is an overview of key concepts including program structure, indentation, identifiers, keywords, constants, variables, and operators.
1. Structure of a Python Program
A Python program typically consists of:
- Statements: Instructions executed by the Python interpreter.
- Functions: Blocks of code designed to perform a specific task.
- Classes: Templates for creating objects (a particular data structure).
- Modules: Separate files containing Python code that can be imported into other programs.
Basic Structure Example:
# Importing a moduleimport math# Function definitiondef greet(name): print("Hello", name)# Main programgreet("Alice")print("The square root of 16 is", math.sqrt(16))
2. Indentation
Indentation refers to the spaces or tabs at the beginning of a line. In Python, indentation is crucial as it defines the structure of the code. Unlike other programming languages that use braces {}
, Python uses indentation to group statements.
Example:
if 5 > 2:print("Five is greater than two.")Incorrect Indentation Example:
if 5 > 2:print("Five is greater than two.") # This will cause an error
Key Points:
- Consistent indentation is necessary.
- Typically, 4 spaces are used per indentation level.
3. Identifiers
Identifiers are names given to entities like variables, functions, classes, etc. in a program.
Rules for Identifiers:
- Can include letters (a-z, A-Z), digits (0-9), and underscores (_).
- Must not start with a digit.
- Are case-sensitive (
name
andName
are different).
Examples:
valid_name = 10_valid_name = 20name2 = 30
Invalid Identifiers:
2name = 10 # Starts with a digit, invalidname@ = 20 # Contains special character, invalid
4. Keywords
Keywords are reserved words in Python that have special meanings. They cannot be used as identifiers.
Some Common Python Keywords:
if
,else
,elif
,for
,while
,break
,continue
def
,return
,import
,from
,as
class
,try
,except
,finally
True
,False
,None
Example:
if True: print("This is a valid use of the 'if' keyword.")
5. Constants
Constants are variables whose values do not change throughout the program. Python does not have built-in constant types, but by convention, uppercase letters are used to denote a constant.
Example:
PI = 3.14159GRAVITY = 9.8
6. Variables
Variables are used to store data that can be changed during program execution. Variables in Python do not need explicit declaration; they are created when a value is assigned.
Example:
age = 21name = "John"
7. Types of Operators
Operators are symbols that perform operations on variables and values. Python supports several types of operators:
a. Arithmetic Operators
- Addition (
+
), Subtraction (-
), Multiplication (*
), Division (/
) - Modulus (
%
): Returns the remainder of a division - Exponentiation (
**
): Raises a number to the power of another - Floor Division (
//
): Division that results in the largest integer less than or equal to the result
Example:
a = 10b = 3print(a + b) # 13print(a - b) # 7print(a * b) # 30print(a / b) # 3.3333print(a % b) # 1print(a ** b) # 1000print(a // b) # 3
b. Comparison Operators
- Used to compare two values and return a Boolean value (
True
orFalse
). - Equal (
==
), Not Equal (!=
) - Greater than (
>
), Less than (<
) - Greater than or equal to (
>=
), Less than or equal to (<=
)
Example:
x = 10y = 20print(x == y) # Falseprint(x != y) # Trueprint(x > y) # Falseprint(x < y) # True
c. Logical Operators
- and: Returns
True
if both statements are true. - or: Returns
True
if one of the statements is true. - not: Reverses the result, returns
False
if the result is true.
Example:
x = Truey = Falseprint(x and y) # Falseprint(x or y) # Trueprint(not x) # False
d. Assignment Operators
- Used to assign values to variables.
- Basic Assignment (
=
), Addition Assignment (+=
), Subtraction Assignment (-=
), Multiplication Assignment (*=
), Division Assignment (/=
)
Example:
x = 5x += 3 # x = x + 3print(x) # 8
e. Membership Operators
- in: Returns
True
if a sequence with the specified value is present in the object. - not in: Returns
True
if a sequence with the specified value is not present in the object.
Example:
fruits = ["apple", "banana", "cherry"]print("banana" in fruits) # Trueprint("grape" not in fruits) # True
Summary
- Structure of a Program: Includes statements, functions, classes, and modules.
- Indentation: Defines the structure of the code.
- Identifiers: Names given to variables, functions, etc.
- Keywords: Reserved words with special meanings.
- Constants: Variables with values that don't change (by convention).
- Variables: Containers for storing data.
- Operators: Symbols that perform operations on variables and values.
0 Comments
Please do note create link post in comment section