Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Introduction to Python

Python Fundamentals
[6]
→ Python Character Set :-

 Character =
 An alphabet, digit or a symbol is called a character.
 Eg : ?,<,#,Y.8.0.@…..etc:-

 Character set =
 Upper case alphabets = A to Z
 Lower case alphabets = a to z
 Digits = 0 to 9
 Symbols = 32 symbols

 Token =
 The basic building block of a program or an indivisible unit in a
program is called as a token.
 They are mainly of 5 types :-
i. Keywords
ii. Identifiers
iii. Literals
iv. Operators
v. Punctuators

 Program elements - List , String, Loop,Array

→ I) Keywords :-

 Keywords are reserved words that are predefined.


 Eg : if, else, elif, true , false , none, for, while, return…etc:-
 A keyword is reserved words whose meaning is already defined by
the python developers and it cannot be used as names of.

→ II) Identifiers:-

 The names given to various program element are called


identifiers.
 Rules for naming identifiers :-
 The first character must be an alphabet or the symbol
underscore “_”.
 NO space is allowed while naming an identifier.
 Keywords should not be used as identifiers.
 The only symbol which can be used is underscore “_”.
 Digits can be used from second character onwards

 Program elements :-
 Variables, functions ,list ,tuples ,string ,dictionary ,classes ,
objects etc:- are called as program elements.

 Examples of valid identifiers :-


 A,simple_interest,area,volume ,_xyz ,_m_n etc:-

 Examples of invalid identifiers :-



NAMES REASON FOR
INVALIDITY
2x Begins with a digit
return Keyword
a-b Symbols cannot be used
m@n#q Symbols cannot be used
none Keyword

→ III) Literals :-

 Literals are the items that have a fixed value.

Literals

Boolean Special Literal Literal


String Numeric
[True / False] [None] collections
 String Literal =
 A string literal is a single or a group of characters enclosed
inside quotes [single/double/triple quotes].
 Eg : ‘ Bhagat Singh ’
“ Arun’s pen is red in colour”
‘’’Chemistry teacher taught “Lutetium” is the most
expensive natural element’’’

 String types :

String

Single - Line Multi - Line


Numeric
String String

 A) Single - Line String = String that terminates in one line


and are enclosed in single double triple quotes are normally
single line strings.

 C) Multi - Line String = Spreading text across multiple lines


is called as multi-line string.

i. Using single/ double quote =


a) If you use single / double quotes to create a multi-line
string, then backslash character should be used at
the end of each line..
b) Eg : A = ‘AK47 \
is manufactured \
in\
Russia’
ii. Using triple quote =
a) If you use triple quotes to create a multi-line string,
then backslash character need not be used at the end
of each line, instead you can use enter key at end of
each line.

 Non Graphic characters :


 Those who does not have any symbol when they can be
typed directly from keyboard are called as non-graphic
characters.
 Eg : delete,backspace, tab , alt,ctrl, enter, fn , space bar
etc:-
 Only some actions take place but no symbols are printed
when these keys are pressed.
 Python allows us to have certain non-graphic characters in
strings. These non graphic characters can be represented
by using escape sequence.

 Escape sequence :
 An escape sequence is a 2 cahracter string starts with a
backslash followed by another character.
 Most frequently used sequences are :-
> \n → new line character
> \t → tab space or horizontal tab
> \a → ASCII bell
> \b → ASCII backspace
> \\ → backspace

 B) Numeric String =
 They are three types of numeric literals in python. They
are : -
i. Integer Literal
ii. Float Literal
iii. Complex Literal

 i. Integer Literal =
 Whole numbers without any fractional part.
 Can be positive or negative.
 There are 3 types of integers :-
 Decimal integers (0 to 9) [ 123,71,59,159 ]
 Octal integers (0 to 7) [ Oo123,Oo71,Oo5,Oo15]
 Hex a-decimal integers (0 to 9)& (A to F)
[ Ox123,Ox71,Ox59,Ox159 ]

 ii. Float/Floating point/Real Literal =


 Numbers having fractional parts , i.e, a decimal point
is mandatory in float literal.
 Eg: Integer literal 17 can be represented in float
literal 17.0
 Fractional form and exponent form are 2 forms of
representing float literals.
 Fractional form =
 It consists of signed or unsigned digits
including decimal parts.
 Eg : 17.81,0.567,-0.004613
 Exponent form =
 A float literal in exponent form consists of 2
parts - namely mentissa and exponent.
 Menitissa must be either an integer or a real
constant.
 It is followed by E/e (10n)

 Boolean literals = A boolean literal in python is used to


represent one of the two boolean value is true or false.

 Special literal None =


 Python has a special literal called none.
 None literal indicated the absence of value.

→ IV) Operators:-

 Operators = A symbol that is used to perform an operation.


 Operand = The values upon which operators acts is called
operand.
 Eg : 23+56 .
Here, 23 and 56 are operands ad ‘+’ is operator.
 Operands can be numbers, variables or expressions.

→ Types of operators :-
i. Arithmetic operators =
 Addition → (+)
 Substraction → (-)
 Multiplication → (*)
 Division → (/)
 It gives quotient as real value.i.e with decimal point/value.
 Eg : 10/3 = 3.33
 Floor Division → (//)
 It gives quotient as integer .i.e without decimal value.
[ Does not round off the value ]
 Eg : 10//3 = 3

 Modulus → (%)
 It gives only remainder when a number is divided by
another number.
 Eg : 10%3 = 1

ii. Relational operators =


 The operators which are used to compare 2 operands are called
relational operators.
 They are :
 Greater than → (>)
 Lesser than → (<)
 Greater than or equal to → (> =)
 Lesser than or equal to → (< =)
 Equal to → (==)
 Not equal to → (!=)

Note :
 If the comparison of relational operators give the result as true then
value 1 / True will be returned.
 If expression is false, then value 0 / False will be returned.
 Eg : >> a = 15 < 10
Here, 0 / False will be returned.
>> b = (100//21) == (2*2)
Here, 1 / True will be returned.
 = → Assignment ; = = → Comparing operators

iii. Assignment operator [ = ] =


 = is used to assign the value on L.H.S to the variable on
R.H.S.
 Eg : Let a = 100 , b = 200
Then x = a % b
y = b // 10
z = (b // a) == (a * 10)
Here, x,y,z are assigned as =
x = 100
y = 20
z = False / 0

iv. Augmented Assignment operators =


 Combining the assignment operator with arithemetic operator results in
augmented assignment operator.
 They are :
 +=
 -=
 *=
 /=
 // =
 %=
 Eg :
 +=
Let a = 3
Then a + = 10 The already existing value will be added with It
will yield 13 a and new value is stored in a itself
a = 13

 -=
Let a = 3 , b = 2
b-=a
a=3-2
a=1

 *=
Let a = 3 , b = 2
a*=b
a=3*2
a=6

 /=
Let a = 250 , b = 25
a /= b
a = 250 / 25
a = 10.0
 // =
Let a = 250 , b = 25
a // = b
a = 250 // 25
a = 10

 %=
Let a = 1000 , b = 400
a%=b
a = 1000 % 400
a = 200

v. Logical operators =
 These are operators which are used two or more relational operators.
 They are :
 Logical and
 Logical or
 ‘And’ → is used for logical and operator.
 ‘Or’ → is used for logical or operator

 Eg :
 Let x = 1000, y = 200 , m = 100 , n = 5
a = ( x < y ) and ( m > n )
a = 0 / False

 Let x = 1000, y = 200 , m = 100 , n = 5


Let b = ( x != m) or ( y == n )
a = 1 / True

→ V) Punctuators:-

 Punctuators are the symbols that are used in programming


languages to organise sentence structures and indicate the
rhythm and emphasis of expressions, statements and program
structure.
 Most commonly used punctuators in python programming language
are :-
‘,”,#,(),[],{},@,,,:,.,=

You might also like