Python Basics
Python Basics
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.
\\ Backslash(\)
\’ Single quotes
\” Double quotes
\n New line
\t Horizontal tab
\v Vertical tab
NUMERIC LITERALS
Numeric int float complex
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:
%
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.
and Returns “True”- if both operands are TRUE >>> True and 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
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.