Synopsis
Cambridge court world school
Class –IX
Subject-AI
Python basics
Python Introduction
Python is a high-level, interpreted, interactive, and object-oriented programming language, known for its
emphasis on readability and simplicity. It was created by Guido van Rossum in the late 1980s and officially
released in 1991.
Here's a synopsis of the core concepts in Python basics:
1. Syntax and structure
Indentation: Python uses indentation (typically 4 spaces) to define code blocks instead of curly braces or
keywords.
Comments: Comments start with a hash symbol (#) for single-line comments. Multi-line comments or
docstrings are enclosed in triple quotes ("""Docstring""" or '''Docstring''').
Keywords: Python has reserved words with specific meanings, like if, while, def, class, etc.,.
Statements: Each line usually represents a statement, and semicolons are allowed but rarely used.
Identifier: In Python, an identifier is a name used to identify an entity such as a variable, function, class,
module, or other object. Identifiers serve to uniquely distinguish one entity from another within a program.
Like variable ,function, classes, modules
2. Variables and data types
Variables: Variables store values and are created by assigning a value to a name, e.g., x = 10. Python is
dynamically typed, so you don't need to declare a variable's type explicitly.
Data Types: Python includes fundamental data types:
o Numeric: Integers (int), floating-point numbers (float), and complex numbers (complex).
o Strings: Sequences of characters enclosed in single, double, or triple quotes ('hello', "world").
o Booleans: Representing True or False.
Collections: Python offers various data structures for storing collections of data:
o Lists: Ordered, mutable collections, defined using square brackets [].
o Tuples: Ordered, immutable collections, defined using parentheses ().
o Sets: Unordered collections of unique elements, defined using curly braces {} or set().
o Dictionaries: Key-value pairs, unordered and mutable, defined using curly braces {key: value}.
3. Operators
Arithmetic Operators: Perform mathematical operations (+, -, *, /, //, %, **).
Comparison Operators: Compare values and return a Boolean result (==, !=, <, >, <=, >=).
Logical Operators: Combine Boolean expressions (and, or, not).
Assignment Operators: Assign values to variables (=, +=, -=, etc.).
4. Control flow
Conditional Statements: Execute blocks of code based on conditions (if, elif, else).
Loops: Repeat code blocks multiple times:
o for loop: A for loop is used to iterate over a sequence (like a list, tuple, string, or range) or other iterable
objects. The loop executes once for each item in the sequence. It is typically used when the number of iterations
is known or determined by the length of the iterable
Syntax:
for item in iterable:
# code to be executed for each item
while loop: A while loop repeatedly executes a block of code as long as a specified condition remains True. It is
used when the number of iterations is not known in advance and depends on a condition being met.
Syntax:
Python
while condition:
# code to be executed while the condition is True.
Control Flow Altering Statements:
o break: Exits the innermost loop prematurely.
o continue: Skips the current iteration and moves to the next.
Variables in Python
In Python, variables are like containers or labels that you use to store and manage data values within your
programs. They are fundamental building blocks for working with information and making your code dynamic
and adaptable.
1. Defining variables
Assignment: You create a variable by assigning a value to a name using the equals sign (=), which is the
assignment operator.
No Explicit Declaration: Unlike some other programming languages, you don't need to declare a variable's data
type (e.g., integer, string) before using it. Python automatically determines the type based on the value you
assign. This is a feature called dynamic typing.
o Example:
python
age = 25 # Python infers 'age' is an integer
name = "Alice" # Python infers 'name' is a string
Use code with caution.
2. Naming conventions and rules
Start with a Letter or Underscore: Variable names must begin with a letter (A-z, a-z) or an underscore (_).
No Numbers at the Beginning: They cannot start with a digit (0-9).
Alphanumeric Characters and Underscores: Variable names can only contain alphanumeric characters (A-z, a-z,
0-9) and underscores (_).
Case-Sensitive: Variable names are case-sensitive. myVariable, myvariable, and MyVariable are all considered
different variables.
No Python Keywords: Avoid using Python's reserved words (keywords) as variable names
(e.g., if, else, while, for).
No Spaces: Variable names cannot contain spaces. Use underscores or camel case to separate words for
readability (e.g., my_variable, myVariable).
Meaningful Names: Choose descriptive names that reflect the variable's purpose to improve code readability.
For instance, use total_price instead of tp
Conditional statements in Python
Conditional statements are essential in Python programming for making decisions and controlling the flow of a
program based on certain conditions. They allow a program to execute specific blocks of code only if a given
condition is true, otherwise, it skips that block or executes an alternative block.
Types of conditional statements
Python supports several types of conditional statements to handle different decision-making scenarios:
1. if statement
The basic if statement executes a block of code only if a condition is true.
Syntax:
if condition:
# Code to execute if the condition is True
2. if-else statement
This statement provides an alternative block of code to run when the if condition is false.
Syntax:
if condition:
# Code to execute if the condition is True
else:
# Code to execute if the condition is False
3. if-elif-else statement
Used for checking multiple conditions sequentially, this structure evaluates elif conditions if the preceding ones
are false, executing the else block only if none of the conditions are true.
Syntax:
if condition1:
# Code block for condition1
elif condition2:
# Code block for condition2
elif condition3:
# Code block for condition3
else:
# Code block if none of the above conditions are true
4. Nested if statements
A nested if statement in Python involves placing one if (or elif, or else) statement inside
another if statement. This structure allows for the evaluation of multiple conditions in a hierarchical manner,
where the inner condition is only checked if the outer condition is true.
Syntax:
if outer_condition:
# Code to execute if outer_condition is true
if inner_condition:
# Code to execute if both outer_condition and inner_condition are true
else:
# Code to execute if outer_condition is true but inner_condition is false
else:
# Code to execute if outer_condition is false