You are on page 1of 24

SHEELA DILIP

Character set- A set of valid characters recognized by python. Python uses the
traditional ASCII character set. The latest version recognizes the Unicode
character set. The ASCII character set is a subset of the Unicode character set.
The Python Character set comprises of:

❖Letters : A–Z ,a–z

❖Digits : 0–9

❖Special Characters : All keys available on the keyboard

❖White Spaces : blank space,tab,carriage return,new line, form feed

❖Other characters : Unicode


ALPHABETS -> WORDS -> SENTENCES
(A,B,a,b..) (Ann,is,an..) (It is a Book)

CHARACTERS -> TOKENS -> STATEMENTS


Token – The smallest identifiable individual unit in a program that is
meaningful to the language interpreter is known as a token.

Words

Nouns Pronouns Verbs Adverbs Adjectives

Tokens

Keywords Identifiers Literals Delimiters Operators


Keywords or also known as Reserved Words are tokens that are reserved for a
specific purpose. So, they cannot be used as identifiers or for any other purpose
other than it is intended to. Some of the commonly used keywords in Python are:
False class from or
None continue global pass
True def if raise
and del import return
as elif in try
assert else is while
async except lambda with
await finally nonlocal yield
break for not
An identifier is a token is used to identify a part of the program. It is used to
name a variable, function, class, module or any other object.
Identifer (Variable) naming conventions:

➢ An identifier can consist of the following characters of the Python Character set:
❖ Alphabets (A – Z, a – z)
❖ Digits ( 0 - 9)
❖ Underscore( _ ) Note: No other special character is allowed
➢ An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or
more letters, underscores and digits (0 to 9).
➢ Identifier must not be a keyword of Python
➢ Python is a case sensitive programming language.
Thus, name and Name are two different identifiers in Python.
Suggestion: Use names that pertains to the purpose of the variable
Some valid identifiers

Mybook file123 z2td


date_2 _no First_Name

Some invalid identifier

2rno break my.book


data-cs Last Name XI-A

An insight into the concept of Python variables


A literal is any “constant” value that can be used in an assignment statement or other
expression.
A literal can be of any type- numeric ( integer, float etc ), or non numeric ( string etc )
A few examples of Literals are:
5 - integer literal (Numeric)
“ Bhavan” - string literal ( Non Numeric )
True - Boolean literal
100.50 - float literal
[10,20,30] - list

a=10
X = 20-10*3
data= [“ Anil”,”Sunil”, “Vinay”]

A literal represents the Value stored in a location.


Operators are tokens which represent a computation. These
computations are done on operands which can be values or variables

Operators

Compound
Arithmetic Relational Logical Assignment Assignment
(Short hand)

Priority Highest Lowest


Operators

Assignment Assignment
Compound
Arithmetic Relational Logical
= Assignment
=
Operands-Numeric(Mostly) Operands - Numeric
Operands-Numeric(Mostly) Boolean Used in an expression
Result -Numeric(Mostly) Result - Numeric
Result -Boolean Result - Boolean

+ (Addition) > [Greater than]


Helps to
+= (Add & Assign)
- (Subtraction)
not assign a
- =(Subtract & Assign)
>[less than] constant,
*(Multiplication) and Variable
Or an
*= (Multiply & Assign)
> =[Greater than Equal
/(Division) expression /= (Assign Quotient)
%
to]

<= [Less than Equal to]


or to a variable
%=(Assign Remainder)
(Remainder/Modul
o) == [Equal to] ** = (Assign the
**(Exponentiation) Exponentiation result)
!= , <> [Not Equal to]
// = Assign the Quotient
//Integer Division of Integer Division
Arithmetic Relational Logical Assignment Short Hand
Operators Operators Operators Operators Operators
L-value = R-Value

Assigning a value
a=5
Assigning a variable
b=a
Assigning an
expression
c=a+b
Note:
The left of an
expression is
always a variable.
The Right may be
a constant value,
variable or an
expression
Special characteristics of + and * operators
These 2 operators behave differently with numeric and string values.

+ Operator *Operator
With numeric values: Performs Addition
Eg: 2 + 3 = 5 With numeric values: Performs Multiplication
3.5 + 2.3 = 5.8 Eg: 2 * 3 = 6
2.3 *3 = 6.9
With Strings: Performs concatenation
Eg: ‘2’ + ’3’ = ’23’ With Strings: Performs Replication
‘Hello’ +’dear ’ = “Hellodear” Eg: ‘2’ * 3 = ’222’
‘Hello’ * 3 = ‘HelloHelloHello’
Based on the number of operands required for the operation,
operators are broadly classified into:
1. Unary ( 1 operand ) Priority Highest
2. Binary ( 2 operands )
3. Ternary ( 3 operands ) Lowest
Unary Operators: Binary Operators:
➢ Arithmetic Operators : ( + - * / % ** //

➢ Relational Operators : ( > , < , >= ,<= , == , not =

➢ Logical Operators : ( and , or )

➢ Assignment Operator : ( = )

➢ Comp. Assignment Op : ( +=,-=,*=,/=,%=,**=, //=)

Data Types
A variable is a named unit of storage. A variable helps to bind names with
objects. Associated with every variable, there are three main components :

Identity
Variable
Type
Value
Identity refers to the address (L-Value)of the variable in the memory which never
changes once created.The address can be retrieved by the id()method. Eg:
Identity
Address of the variable ‘a ‘ in the memory
Variable

Type refers to the data type of the variable. Data type refers to the type of
the variable and its associated operations on it. Python provides a variety
Type of data types which will be referred in the next slide. The type() returns the
data type of the variable in the memory. Eg:

Value (R-Value) refers to the value assigned to the variable. The


Value Assignment operator(=) helps to bind a value to a variable . This process
os also termed as Building of a Variable. Eg:
Literals
Syntax:
L-value = R-Value
L-Value must always be a Variable
R-Value may be a variable, constant or an expression

Assigning a value A=5 A=5


Assigning a variable B=A B=5
Assigning an expression A= B+2*5 A=15
Multiple Assignments A,B,C=1,2.0,”abc” A=1, B=2.0,C=abc
Multiple Assignments A=B=C=5 A=5 B=5 C=5
Multiple Assignments A=1;B=2;C=3 A=1, B=2,C=3

Assigning comma separated values a=1,2,3 (1,2,3)->Tuple

Multiple Assignments-strings a,b,c="pqr" a=p , b=q , c=r


Examples of Invalid Assignments
a,b=1,2,3 Error:too many values to unpack
ValueError: not enough values to unpack
a,b,c=1,2
(expected 3, got 2)

a,b,c=5 cannot unpack non-iterable int object

a,b,c="pq" not enough values to unpack


a,b,c="abcd" Error:too many values to unpack

Inference: No.of variables in the left must be equal to the no. of


values to the right of the Assignment Expression
Some interesting facts on variable assignments
❑ >>> a,b,c=10,a+10,b+20 >>> a,b=100,200
Traceback (most recent call last): >>> a,b,c=10,a+10,b+20
File "<pyshell#0>", line 1, in >>> print(a,b,c)
<module>
a,b,c=10,a+10,b+20 OUTPUT:
NameError: name 'a' is not defined 10 110 220
❑ >>> a=100,b=200
SyntaxError: cannot assign to literal
Correction:
a,b=100,200
Data Types are means to identify the type of data and its associated operations on it
Statements

Non-Executable
Executable
(Comments)

Single line Multi line Single line Multi line


Instructions that a Python interpreter can execute are called Executable statements.
Examples of Single Line Statements:
Eg:
A=5
Eg:
Print(A)
In Python, the end of a statement is marked by a newline character. But we can make a
statement extend over multiple lines with the line continuation character (\) or ( ).
Examples of Multi Line Statements:
Comments are non executable statements in a program
Single-line comments are created simply by including a hash (#) character, and they are automatically terminated by the
end of line.
For example:

# this is one line comment


A=5 # Assignment statement
The multiline comment is used to comment more than one line. It is used to write comments in paragraph and
at a time you can comment many lines.The comment is enclosed within triple single quotes( ‘ ‘ ‘ ‘ ‘ ‘) or
triple double quotes(“”” “””)
Example:
''' This is multi line comment in python This program is for
calculating the sum of two numbers and then print the output on
python console '''

Note: In the format menu


Select Comment Out Region (Alt+3) to comment the required area
Select uncomment Region(Alt+4) to uncomment the commented area.

You might also like