ALCHEMY SCHOOL
CLASS-XI
INFORMATICS PRACTICES
SUBJECT CODE: 065
Prepared By:-
Jyoti Verma
CHAPTER - 2
PYTHON FUNDAMENTALS
SET
Python character set represents the set of valid characters that Python supports. It
has the following character set
Letters A–Z, a–z
Digits 0–9
Special Symbols Space + – * / ** \ { } ( ) // = != == < , > . ' ' " " ; : % !
& # <= >= @ _ (underscore)
Whitespace Blank Space, Tab, Newline
Other Characters Python can process all ASCII and Unicode
characters as
part of data or literals
TOKENS
The smallest individual unit in a program is known as Token. It is also called
as Lexical Unit. Tokens present in Python are (i) Keywords (ii) Identifiers (iii)
Literals (iv) Operators (v) Punctuators
Keywords: A keyword is a reserved word that has a predefined meaning to
the compiler / interpreter. A keyword must not be used as identifier. Python
has the following keywords
False assert del for in or while
None break elif from is pass with
True class else global lambda raise yield
and continue except if nonlocal return
as def finally import not try
CONTD…
Identifiers: An identifier is a name given to a program element such as variable,
function, list, dictionary etc. The rules to be followed while naming an identifier
in Python are,
❖ It may consist of letters (A–Z, a–z), digits (0–9), and underscore( _ )
❖ It must begin with a letter or an underscore
❖ It must not begin with a digit
❖ Uppercase and lowercase alphabets are different. For example sum, Sum,
SUM all are different
❖ A keyword must not be used as an identifier
❖ It can be of any length. However, it is preferred to be short and meaningful
CONTD…
Literals / Constants: A literal or constant is a program element that will
never change its values during program execution. Python allows several
kinds of literals like (i) String literals (ii) Numeric literals (iii) Boolean
literals (iv) Special Literal None
String Literals: A string literal is a sequence of characters enclosed in
either single quotes or double quotes. Either both single quotes or both
double quotes to be used for a string. Example: "Python", 'Program' etc.
A single quoted string inside double quotes and vice–versa is legal in
Python. Ex: "Anu's" and 'Anu"s' are valid
Escape sequence will be given as a string and performs specified
task. Some escape sequences are
\n New line character
\t Horizontal Tab
CONTD…
Python allows (i) Single–line Strings (ii ) Multiline Strings
Single–line Strings: The strings that create by enclosing text
in single quotes or double quotes are called single–line
strings. Ex: "Python", 'Apple'
Multiline Strings: To provide a string in multiline, it is to be
provided in triple quotes (triple single quotes or triple double
quotes)
Ex: print ("""Jawahar
Navodaya Vidyalaya""")
CONTD…
Numeric Literals: These literals are three types namely (i) Integer
literals (ii) float literals (iii) complex literals
Integer Literals: An integer constant must have at least one digit
and must not contain any decimal point. Different integer literals
available are,
1. Decimal Integer Literals: It consists of a sequence of digits
between 0 and 9 and does not start with zero. Ex: 1234, –458 etc
2. Octal Integer Literals: It consists of a sequence of digits between
0 and 7. It begins with 0o(Digit Zero Letter o) Ex: 0o24, 0o746 etc
3. Hexadecimal Literals: It consists of a sequence of hexadecimal
values between 0–9 and A–F. It begins with 0x. Ex: 0x14AC
CONTD…
Floating Point Literals: These are also called as Real Literals. These can
be expressed in two forms viz. Fractional Form and Exponent Form
1. Fractional Form: A real constant in fractional form must have at least
one digit either before or after decimal point. Ex; 2.0, 17.5, –14.6, –
0.05, .3 (means 0.3), 6. (means 6.0)
2. Exponent Form: A real constant in exponent form consists of two parts
mantissa and exponent. The mantissa must be either an integer or a
proper real constant. The mantissa is followed by a letter E or e and the
exponent. The exponent must be an integer.
Ex: 152E05, 1.52e07, 0.152E08, 152e+8,–0.172E–3, .25e–4
Boolean Literals: A Boolean literal represent one of the two Boolean
values i.e. True or False. A Boolean literal can either have value as True
or as False
CONTD…
Special Literal None:
None is a special literal in Python. It indicates the absence of
value. It means "There is not useful information" or "There is
nothing here“
Ex: >>>a = None
>>>print (a)
None
CONTD…
Operators: An operator is a symbol that is used in a program in respect of some
operation. Each operation is denoted by some operator. For example the operation
addition is denoted by + and the operation “finding remainder” is denoted by %.
Each programming language will have its own set of operators.
The constants or variables that participate in the operation are called
operands
Unary Operator: If an operator takes only one operand then it is called Unary Operator
Binary Operator: If an operator takes two operands then it is called Binary Operator.
Ternary Operator: If an operator takes three operands then it is called Ternary Operator
Punctuators: These are the symbols used in programming to organize sentence
structures and indicate the emphasis of expressions, statements and program structure.
Some punctuators available in Python are,
'"#\()[]{}@,:.=
BASIC STRUCTURE OF PYTHON
PROGRAM
The following is a simple Python program
1. Expression: An expression is any valid combination of symbols and
represents a value. Eg: 2.3
2 * 3/5
2. Statement: A statement is a programming instruction that does something i.e.
performs some action.
eg: print(‘hello’)
if a>5:
3. Comments: These are the statements that will be ignored by Python
interpreter and will increase readability of program. A single line comment starts
with the symbol #. # colon means it requires a block. For single line comments
# statement 1
# statement 2 for multiple comments
# statement 3
4. Functions: A function is a code that has collection of statements to do some task.
It has a name and a function can be executed repeatedly as many times required.
Eg:
5. Blocks and Indentation: A group of statements which are part of another
statement or a function are called block or code–block or suite.
VARIABLES
A variable is a program element that can change its value during
program execution.
Ex: age = 20 # Means variable age is integer
average = 95.6 # Means variable average is of type float
name = "CBSE"# Means variable name is of type string
CONTD…
Multiple Assignments: Different ways of assignments are,
1. Assigning same value to multiple variables
Ex: a = b = c = 18
2. Assigning multiple values to multiple variables
Ex1: x, y, z = 10, 20, 30 # Means x=10, y=20, z=30
Ex2: x,y = y,x # This makes x=20, y = 10
Ex3: a, b, c = 5, 10, 7
b, c, a = a+1, b+2, c–1
print (a, b, c) # a=6, b=6, c=12
Here, first evaluations of RHS expression(s) and then assigns them
to LHS
CONTD…
Dynamic Typing: A variable having a value of certain data type
can be assigned with value of some other data type. In this case,
it automatically assumed to change the data type of that variable.
This is referred as Dynamic Typing
x = 10
print (x)
x = "Informatics Practices"
print(x)
This code will results in
10
Informatics Practices
Thank
you