You are on page 1of 26

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

 float (Floating Point Real numbers) : Real


numbers written with a decimal point.
Eg:2.5,-13.56

 Complex (Complex Numbers): In the form


a+bj. Where ‘a’ is the real part of the number
and ‘b’ is the imaginary part.
 Integer literals are whole numbers without any
fractional part.
 It may contain either (+) or (-) sign. A number
with no sign is assumed to be positive.
 Python allows three types of integer literals:
◦ 1.Decimal Integer literals: 0-9
◦ 2.Octal Integer literals: represented by 0o
Eg: 0o8
◦ 3.Hexadecimal Integer literals: represented by 0x
Eg: 0xC
Note: Binary Literals are also possible in python,
represented by 0b. Eg: 0b11
 Floating point literals are also called as real literals.
Real literals are numbers having fractional part.
 Floating point literals can be of two forms
◦ 1. Fractional Form: Consist of signed and unsigned digits
including a decimal point. Eg: 3.5
◦ 2. Exponent Form: having two parts-mantissa and
exponent. Eg: 6.5 X 102 =6.5E2
◦ Here 6.5 is mantissa and exponent part is 2
◦ Mantissa can e an integer or floating point value
 Used to represent one of the two boolean
values i.e True or False.
 A boolean literal can either have value as True
or as False.

Special Literal
None: Used to indicate absence of value.

Literal Collections: It include Tuples


and Lists
 Definition: Operators are tokens used for some
computation in an expression.
Operators used in Python
❖ Unary Operator ( Require only one Operand)
Unary Plus (+), Unary Minus(-), Bitwise Complement(~),
Logical Negation (not)
❖ Binary Operator ( Require two Operands)
• Arithmetic Operators : +, -, *, /, %, **, //
• Relational Operators : <, >, <=, >=, ==, !=
• Logical Operators : and, or
• Assignment Operators : =,/=, +=, *=, %=,-=, **=, //=
• Bitwise Operators : &, ^, |
• Shift Operators: <<, >>
• Identify Operators: is, is not
• Membership Operators : in, not in
 Definition: Symbols used to organize
sentence structures in a programming
language.

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

❖ Assigning multiple values to multiple


variables
Eg: a,b,c=10,20,30
Definition: A variable pointing to a value of a
certain type, can be made to point to a value
of a different type. This is called Dynamic
Typing.
Eg: a=10
print (a)
a=‘hello’
print (a)
a =10
Loc:2000
a 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>])

*objects means it can be one or multiple comma separated objects to


be printed.
Eg:
print(“hello”)
a=10,b=20
print(a,b)
 A print() statement without any value or name
or expression prints a blank line.
 A print statement auto converts the items to
strings
 A print() statement inserts spaces between
the items automatically because by default
the value of sep argument is space.
 It appends a newline character at the end of
the line unless you give your own end
argument
Output
Output

You might also like