Python XI Chapter 5
Python XI Chapter 5
Python Fundamentals
➢ Python Character Set
➢ Tokens
▪ Keywords
▪ Identifiers
▪ Literals
▪ Operators
▪ Punctuators
➢ Variables and Assignments
➢ Input and Output
Definition : Character set is a set of valid
characters that a language can recognize.
Python Character Set:
Letters -> A-Z, a-z
Digits -> 0-9
Special Symbols -> + - * & = etc.
White Spaces-> Blank Space,tab,new line etc.
Other Characters-> ASCII and Unicode
Characters
Tokens/ Lexical Units /Lexical Elements
Definition: The smallest Individual unit in a
program is know as a Token
Python Tokens
✓ Keywords
✓ Identifiers
✓ Literals
✓ Operators
✓ Punctuators
Definition: Keywords are Reserved words that
convey special meaning to the language
compiler/interpreter.
Eg: for if else def elif import while
Definition: Names given to different parts of a
program.
Eg: Variable name, Class Name, Function name
etc.
Rules for Identifier formation
First character must be a letter or an underscore (_)
Upper and lower case letters are different(Case
Sensitive)
Must not be a keyword
Cannot contain any special characters except
underscore
Definition:Data items that have a fixed value.
Python Literals:
▪ String Literals
▪ Numeric Literals
▪ Boolean Literals
▪ Special Literal
▪ Literal Collections
Definition: Sequence of characters surrounded
by quotes
Eg: ‘a’, ‘abc’, “abc”
Types:
▪ Single Line String: must terminate in one line
Text = ‘hello’
▪ Multiline String: Multiple lines can be used
Text= ‘hello\
world’
Text = ‘’’hello
world’’’
int (Integers) : Positive or Negative whole
numbers with no Decimal Point.
Eg: 1234, 45, -52
Special Literal
None: Used to indicate absence of value.
Eg: ‘ “ # [] {} () @ , :
➢ Expressions : Any legal combination of
symbols that represent a value
➢ Statements: Programming instructions
➢ Comments : Additional readable information
to clarify the source code.
• Single line comment – by using #
• Multiline Comment – by using Triple Quotes(‘’’)
➢ Functions: Named code section and can be
reused
➢ Blocks or Suite : Group of statements which
are part of another statement or function.
Definition: Variable is a named storage
location, whose values can be used during
the program run.
To create a variable , just assign to its name
the value of appropriate type.
Eg: City= ‘Bhopal’
Age = 20
Mark = 95.5
City ‘Bhopal’
Age 20
Mark 95.5
Lvalue: expression that can come on the
lhs(left hand side) of an assignment
Rvalue: expressions that can come on the
rhs(right hand side) of an assignment.
A=30
F=45
Illegal assignments
20=a
35=b
a+2=d
❖ Assigning same value to multiple variables
Eg: a=b=c=10
a = ‘hello’
Loc:2000
10
a
Loc:2006
‘hello’
Input :To get input from the user, input()
function can be used.
Syntax:
Variable_name = input(<Prompt>)
Eg:
name = input(“What is your name?”)
➢ The input() function always returns a value of
string type
➢ Int() and float() functions can be used along
with input() function to convert the values to
integer and float types.
Output:
print() function is used to send output to the
output device.
Syntax:
Print(*objects,[sep=‘’or<seperator-string>end=‘\n’ or <end_string>])