CBSE Computer Science Class 11 Python Fundamentals
Elementary Concepts of Python
The fundamentals of Python include its syntax, data types, operators, control flow, functions, and data structures. These elements form the basic building blocks for writing programs, and Python’s straightforward design makes them relatively easy for beginners to learn.
Syntax and basic concepts
Indentation: Python uses whitespace and indentation to define the scope of code blocks, unlike many languages that use braces ({}). Correct indentation is mandatory and critical for the program to function.
Comments: A hash symbol (#) denotes a single-line comment, which is text that the Python interpreter ignores. Multi-line comments can be created by using multiple hash symbols or with triple-quoted strings.
Input and Output: The print() function is used to display output, while the input() function prompts a user for text input from the console.
Variables and data types
Variables: Variables are names that act as references to values stored in memory. A value is assigned to a variable using the equal sign (=).
Dynamic typing: Python is dynamically typed, which means you do not need to declare a variable’s data type. The interpreter infers the type at runtime based on the value assigned.
Basic data types:
Integers (int): Whole numbers, like 10, -5, 42.
Floating-point numbers (float): Numbers with a decimal point, like 3.14, -0.5.
Strings (str): Sequences of characters, enclosed in single or double quotes, like “Hello World” or ‘Python’.
Booleans (bool): Represents truth values, either True or False.
Operators
Operators are special symbols that perform operations on variables and values.
Arithmetic operators: Perform mathematical calculations, such as + (addition), – (subtraction), * (multiplication), / (division), % (modulus), and ** (exponentiation).
Comparison operators: Compare two values and return a boolean result. Examples include == (equal to), != (not equal to), > (greater than), and < (less than).
Assignment operators: Assign values to variables, such as = (assignment) and += (add and assign).
Logical operators: Combine conditional statements. The main logical operators are and, or, and not.
Control flow
Control flow statements determine the order in which a program’s instructions are executed.
Conditional statements (if, elif, else): Allow code to execute different blocks of code based on whether specific conditions are True or False.
Loops (for, while): Enable a block of code to be executed repeatedly. A for loop iterates over a sequence, while a while loop continues as long as a certain condition is met.
break and continue: These statements alter the flow of a loop. break exits the loop entirely, while continue skips the rest of the current iteration and proceeds to the next.
Functions
A function is a reusable block of code that performs a specific task.
Defining a function: Functions are defined using the def keyword, followed by the function name and parentheses.
Arguments and parameters: Functions can accept data, called arguments, through parameters listed in the parentheses.
The return statement: This statement is used to exit a function and pass a value back to the caller.
Data structures
Python has several built-in data structures for storing and organizing data efficiently.
Lists: Ordered, mutable collections of items, created with square brackets ([]).
Tuples: Ordered, immutable collections of items, created with parentheses (()).
Sets: Unordered collections of unique items, created with curly braces ({}).
Dictionaries: Unordered collections that store data as key-value pairs, created with curly braces ({}).
