Getting Started With Python class 11
Getting Started With Python class 11
Class 11 Notes
Introduction to Python
Python is a high-level, general-purpose programming language. Python can be
used in a wide range of applications, from website development and data
analysis to machine learning and automation.
Features of Python
Python is a high-level language. It is a free and open-source language.
It is an interpreted language, as Python programs are executed by an
interpreter.
Python programs are easy to understand
Python is case-sensitive. For example, NUMBER and number are not
same in Python.
Python is platform independent, means it can run on various operating
systems and hardware platforms.
Python has a rich library of predefined functions.
Python is also helpful in web development. Many popular web services
and applications are built using Python.
Executing a simple “hello world” program
To execute a simple ―hello world‖ program in Python, write the following program.
print("Hello, World!")
Execution Modes
There are two ways to use the Python interpreter:
Interactive mode
Script mode
Interactive Mode
Programmers can quickly run the commands and try out or test code without
generating a file by using the Python interactive mode, commonly known as the
Python interpreter or Python shell.
Script Mode
In the script mode, a Python programme can be written in a file, saved, and then
run using the interpreter.
Python scripts are stored in files with the ―.py‖ extension. Python scripts are
saved by default in the Python installation folder.
Identifiers
Identifiers are names used in programming languages to identify a variable,
function, or other things in a programme. Python has the following guidelines for
naming an identifier:
The name should begin with an uppercase or a lowercase alphabet or an
underscore sign (_).
It can be of any length.
It should not be a keyword or reserved word.
We cannot use special symbols like !, @, #, $, %, etc.
Literal in Python
In Python, a literal represents a fixed value directly using the program. It is raw
data assigned to a variable. Literals are immutable, meaning the value of a literal
cannot be changed during program execution.
Variables
A variable is used to hold the values. In Python, the term ―variable‖ refers to an
object, where a variable points to objects in memory. A variable‘s value can be a
string, such as ―Amit‖, or a number, such as ―345‖, or any combination of
alphanumeric characters (CD67). In Python, we can create new variables and
give them particular values by using an assignment operator (=).
gender = ‗M‘
message = ―Keep Smiling‖
price = 987.9
Example,
L – Value: This refers to the variable name which helps to hold the R –
Values. Example, x = 10 (‗x‘ is the l-value)
R – Value: This refers to the actual data or object being assigned to the l-
value. Example, y = x + 5 (‗x + 5‘ is the r-value)
Comments
In the source code, comments are used to add remarks or notes in the program.
The interpreter uses comments to make the source code simpler and easy to
understand for the programmer. Comments always start with #.
Example,
Data Types
In Python, each value corresponds to a certain data type. A variable‘s data type
describes the kinds of data values it can store and the types of operations it can
execute on that data. The data types that Python.
Number
Only numerical values are stored in the Number data type. Three other
categories are used to further categories it: int, float, and complex.
Sequence
An ordered group of things, each of which is identified by an integer index, is
referred to as a Python sequence. Strings, Lists, and Tuples are the three
different forms of sequence data types that are available in Python.
None
A unique data type with only one value is called None. It is employed to denote a
situation‘s lack of worth. None is neither True nor False, and it does not support
any special operations (zero). example myVar = None
Mapping
Python has an unordered data type called mapping. Python currently only
supports the dictionary data type as a standard mapping data type.
Expressions
Expressions are fundamental building blocks of Python code. They are a
combination of values, variables, operators, and function calls that the interpreter
can evaluate to produce a result.
For example:
Order of
Operators Description
Precedence
Statement Output
print(“Hello”) Hello
print(10*2.5) 25.0
Type Conversion
Type conversion is also known as type casting in Python. Type conversion refers
to the process of changing a value from one data type to another. Python
supports two main types of type conversion:
1. Explicit Conversion
2. Implicit Conversion
Explicit Conversion
Explicit type conversion, also known as type casting, is used when a programmer
manually converts a value from one data type to another using a built-in function,
such as int(), float(), or str().
print(num_int) # Output: 10
print(type(num_int)) # Output: <class 'int'>
Implicit Conversion
num_int = 5 # Integer
num_float = 2.5 # Float
result = num_int + num_float
1. Syntax errors
2. Logical errors
3. Runtime errors
Syntax Errors
When the code does not follow the rules of the programming language. Example:
missing parentheses in Python.
Logical Errors
Logical errors do not crash the program but generate incorrect output due to
incorrect logic.
x = 10
y=5
A runtime error occurs when the program is running, and an error is generated
due to invalid operations like dividing by zero.