PY THON BA S ICS
GRADE 11
INDRAJA ALAND
INTRODUCTION
Guido Van Rossum in 1991.
It was developed by ________________
Syntax:
Set of rules that controls the structure of sentences
in a program.
Basic structure of program depends on IPO cycle.
PYTHON CHARACTER SET
It is a set of valid characters that can be used to write a
source program.
Characters used belong to Unicode standard.
It includes:
Alphabets A-Z or a-z
Digits 0-9
Special symbols + - ( ) * ** & # <= >= _ ! ; : “ ”
etc
White space Blank space, tab space, \n \t \r
etc
PYTHON TOKEN
Eg: “I am learning python. Hurray! It fun.”
Letters, words, punctuations symbols-Lexical units/Token
Smallest individual unit in a program.
Python has following tokens:
Identifiers
Keywords
Literals
Operators
Punctuators/ Delimiters
KEYWORDS
They are predefined reserved words that have special
meaning.
Reserved for special purpose.
Cannot be used as variables.
False None break in
True with class is
Del while def elif
if import continue as
and for from global
IDENTIFIERS
A name used to identify a variable, function, class or
module.
Rules for identifiers:
a. Starts with letter A to Z or a to z or underscore ( _ )
b. Cannot start with digit.
c. Cannot use Keywords.
d. Cannot use special symbols like ( !, @, #, $, % )
Ex: Valid identifiers: Age1, age1, age, a, _age, _Age1, _age1.
Ex: Invalid identifiers: 123age, !age, age!, 12add,21sub.
IDENTIFIERS
Examples:
i. 123sub: Invalid Identifier
ii.Name: Valid
iii.name@123: Invalid Identifier
iv.if name: Invalid Identifier
v.True result: Invalid Identifier
LITERALS/ CONSTANTS
Literalsin Python can be defined as number, text, or other
data that represent values to be stored in variables.
Python supports several literals:
String Literal
Numeric Literals
Boolean Literals
Special Literals
STRING LITERALS
It is collection of characters enclosed in a double or single quotes.
Eg: “Python” , “Hello world” , “Addition of 2+3 is:”
Single-line string literals:
It has only one line. Single /Double quotes used.
Eg: a=‘Hello’
print (a)
Multi-line string literals:
It has multiple lines. Triple quotes used.
Eg: a=‘’’Hello.
welcome back’'
ESCAPE SEQUENCE
The sequence of characters used after backslash (\) is called as
escape sequence.
Escape sequence allows you to include special characters in strings.
Eg: print('anna's hair are long')
OUTPUT: syntaxerror: unterminated string literal (detected at line 1)
LIST OF ESCAPE CHARACTERS
Escape Sequence What it does
\\ Backslash(\)
\’ Single quotes
\” Double quotes
\n New line
\t Horizontal tab
\v Vertical tab
NUMERIC LITERALS
Numeric int float complex
int float complex Numbers Numbers can A pair of real
can be be integer or and imaginary
Decimal either fraction. numbers.
positive or Eg: Eg:
Octal
negative. >>> a= 1.8 >>> a=x+yj
Hexadecimal
Eg: >>> b= -2.1 x: real
>>>a=21 y: complex
>>>b=-34 j: imaginary
>>> a=2+2j
NUMERIC LITERALS
Types of Integer Literals:
a) Decimal : 1234, -50, +100
b) Octal : It starts from symbol 0o (zero followed by letter ‘o’)
For e.g. 0o10 represent decimal 8
>> num = 0o10
>>> print(num)
It will print the value 8
c) Hexadecimal : it starts from 0x (zero followed by letter ‘x’)
>>> num = 0xF
>>> print(num)
It will print the value 15
BOOLEAN LITERALS
A Boolean literals in Python is used to represent one of the two Boolean values
i.e. True or False
Eg:
>>>isMarried=True
>>> type(isMarried)
OUTPUT: <class 'bool'>
__________
>>> a=15>2
>>> print(a)
OUTPUT: True
SPECIAL LITERALS-NONE
Python has one special literal, which is “None”.
It indicates absence of value/ no value.
Not same as 0 or FALSE or EMPTY.
It is also known as NULL.
Eg:
>>> balance=None
>>>print(balance)
OUTPUT: None
___________
>>> salary=None
>>>type((salary)
OUTPUT: <class ’None Type’>
OPERATORS
Operators can be defined as symbols that are used toperform operations on
operands.
Eg:
Arrangement of operands and operators is known as Expression.
Operators can be classified as:
a. Unary operators: eg-a=12,a=-12
b.Binary operators: eg- sum=a+b, mul=a*b
OPERATORS
Types of Operators
1. Arithmetic Operators.
2. Relational Operators.
3. Assignment Operators.
4. Logical Operators.
5. Bitwise Operators
6. Membership Operators
7. Identity Operators
ARITHMETIC OPERATORS
They are used to perform mathematical operations.
Operator Usage Example Output
+ Addition >>> 2+2 4
- Subtraction >>> 4-2 2
* Multiplication >>> 2*2 4
/ Divide >>> 12/2 6
%
Provides remainder >>> 12%6 0
(Modulus)
//
(Floor Provides Quotient >>> 5//2 2
Division)
**
Power to/ raise to >>> 4**2 16
(Exponent)
RELATIONAL OPERATORS
They are used to compare values. They either return True or False.
Operator Usage Example Output
> Greater than >>> 3>5 False
< Less than >>>2<5 True
== Equal to >>>2==1 False
!= Not equal to >>> 2!=3 True
>= Greater than equal to >>> 21>=23 False
<= Less than equal to >>> 21<=23 True
LOGICAL OPERATORS
Three logical operators: and, or and not.
Operator Usage Example Output
and Returns “True”- if both operands are TRUE >>> True and True True
Returns “True”- if either of the operand is
or >>> False or True True
TRUE
not Returns “True”- if the operand is FALSE >>> not True False
ASSIGNMENT OPERATOR
The assignment operator(=) is used to assign a value to an identifier.
Compound assignment- assign and perform operation together.
Operator Usage Example Output
== Assign a value a==2 a=2
x=5
+= Assign addition x += 3 8
print(x)
/= Assign a quotient a/=2 a=a/2
-= Assign subtraction a-=2 a=a-2
*= Assign multiplication a*=2 a=a*2
**= Assign exponent a**=2 a=a**2
//= Assign Floor division a//=2 a=a//2
IDENTITY OPERATOR
To check if two objects are same or not.
Operator Usage Example Output
x=2
y=4
z=2 True--1
is Returns “TRUE” if operands are identical False--2
print(x is z)--1 False--3
print(x is y)--2
print(x==y)--3
x=2
y=4
z=2 False--1
Returns “TRUE” if operands are not
is not True--2
identical
print(x is not z)--1 True--3
print(x is not y)--2
print(x!=y)--3
MEMBERSHIP OPERATOR
They are used to test if a sequence is presented in an object
Operator Usage Example Output
True- if value/variable is found in the x="Global"
in True
sequence print("b" in x)
True- if value/ variable is not found in x="Global"
not in True
sequence print("s" not in x)
BITWISE OPERATOR
In Python, bitwise operators are used to perform bitwise calculations on
integers.
The integers are first converted into binary and then operations are performed
on each bit or corresponding pair of bits, hence the name bitwise operators.
The result is then returned in decimal format.
They are used to compare (binary) numbers.
Operator Name Description Example
Result bit 1,if both operand bits are 1;otherwise
& Bitwise AND x&y
results bit 0.
a=2
a = 10 = 1010 (Binary) b=3
b = 4 = 0100 (Binary) print(a&b)
print(a&b) Output: 2
Output: 0
BITWISE OPERATOR
Operator Name Description Example
Result bit 1,if any of the operand bit is 1; otherwise
| Bitwise OR x|y
results bit 0.
a=2----- > 010
b=3----- > 011
print(a|b)----> 011
Output: 3 (Decimal)
~ Bitwise NOT inverts individual bits ~x
a=2---- > 010
b=3
print(~a) ---- >- 011
Output: -3 (Decimal)
BITWISE OPERATOR
Operator Name Description Example
Results bit 1,if any of the operand bit is 1 but not
^ Bitwise XOR x^y
both, otherwise results bit 0.
a=2 ---- > 010
b=3 ---- > 011
print(a^b) ---- > 001
Output: 1 (Decimal)
Bitwise right The left operand’s value is moved toward right by
>> x>>
shift the number of bits specified by the right operand.
a=11 ----- > 1011
print(a>>1) ----- > 0101
Output: 5 (Decimal)
BITWISE OPERATOR
Operator Name Description Example
Bitwise left The left operand’s value is moved toward left by the
<< x<<
shift number of bits specified by the right operand.
a=11 ------ > 1011
print(a<<1) ----- > 0001 0110
Output: 22 (Decimal)
PUNCTUATORS AND DELIMITERS
Punctuators are symbols used to implement the syntax and structure of
program.
They are tokens and indicate specific purpose in syntax.
Common punctuators are:
, “ # $ @ [ ] { } = : ; ( ) .
All special symbols or punctuators act as delimiters.
Most common delimiters are: comma, tab and colon.
SPLIT FUNCTION
Used to split or break a data item.
Eg:
x="Hello World Python" x="Hello World Python"
a= x.split() a= x.split('o')
print(a) print(a)
Output:
Output: ['Hell', ' W', 'rld Pyth', 'n']
['Hello', 'World', 'Python']
VARIABLES
It refers to reserved memory locations where you can store value to be
used in the program.
Its value can change during the course of program.
Python is dynamically typed language.(no need to declare variable before
they are used)
Also called as “named labels”
a = 2 (a: Lvalue, 2: Rvalue)
Eg:
Example Output
>>> a=b=c=1
1 1 1
>>>print(a,b,c)
>>>a,b,c=3,62,“Hi”
3 62 Hi
>>>print(a,b,c)
COMMENTS
They are text notes added to the program to provide explanatory
information about the program.
They make program easy to understand by another person.
RULES FOR ADDING COMMENTS:
i. Provide meaningful comments.
ii.Write short comments.
iii.Simple and polite.
iv.To the point.
Two types of comments:
a. Single-line comment
b. Multiline comment
SINGLE-LINE COMMENT
Comment is written in single line.
Hash (#) symbol is used in python.
Eg:
# Program will give output as: Hello
print("HELLO")
Output:
HELLO
a=21
b=15
c=a*b # Multiplication of 2 nos. will be done
print (c)
OUTPUT:
315
MULTILINE COMMENT
Comment is written in multiple line.
Hash (#) symbol or triple quotes are used in
# Simple program
python.
#For multplication
Eg: """ a,b,c are variables
# Program will give output as: Hello c stores product of 2
#Program is done on string nos."""
print("HELLO")
a=21
Output: b=15
HELLO c=a*b
print (c)
OUTPUT:
315
INPUT( )
It is used to accept input/ data from user.
Output is directly shown on IDLE or interactive screen.
It always returns a value as a string type.