0% found this document useful (0 votes)
7 views53 pages

Python Fundamental

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
7 views53 pages

Python Fundamental

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 53

Ch-3

Python
FUNDAMENTALS
3.1 OUTLINE
1. Introduction
2. Python Character Set
3. Tokens
4. Barebones of a Python Program
5. Variables and Assignments
6. Simple Input and Output
3.2 P y t h o n C h a r a c t e r
set
Character set is a set of valid characters that a language can
recognize.
Python has the following character set :
□ LETTERS : A-Z , a-z
□ DIGITS : 0-9
□ Special Symbols: space + - * / () {} [] etc.
□ Whitespaces : Blank space, tabs, newline etc.
□ Other character : Python can process all ASCII and
Unicode character as part of data or literal
3.3 T o k e n s
The smallest individual unit in a program is known as a TOKEN
or LEXICAL unit.
Python has following tokens:-
1. Keywords
2. Identifiers(Names)
3. Literals
4. Operators
5. Punctuators
Let us talk about these one by one
3.3.1 K e y w o r d s
A keyword is a word having special meaning reserved by
programming language.
Examples:
•False
•Break
•If
•Elif
•While
•lambda
3.3.2 I d e n t i f i e r s
(Names)
Identifiers are fundamental building blocks of a program and are
used as the general terminology for the names given to different
parts.
Identifiers forming rules are:-
• The first character must be a letter ;underscore counted
• Upper and Lower case letters are treated differently
• Digits can be a part of identifiers 0-9 , but not in the first place
• Identifiers are unlimited in length
• An Identifier must not be a keyword
• An identifier cannot contain any special character except underscore
Examples
Valid:-
•Myfile
•Data9_987_0
•_CHK

Invalids:-
•DATA-REC
•29CLCT
•break
•My.file
3.3.3 L i t e r a l s / V a l u e s

Literals are data items that have a fixed value .


Python allows several kind of Literals :-
1. String Literals
2. Numeric Literals
3. Boolean Literals
4. Special Literal None
5. Literals Collection
3.3.3A S t r i n g L i t e r a l s
In Python , one can form string literals by enclosing text
in both forms of quotes-Single quotes and Double
quotes . A string literal is a sequence of characters
surrounded by quotes
Examples:-
‘Astha’ “Rizwan”
‘1-d-4f-5’ “11234leggvgn45”
Sting types in python
1.Single line strings-That terminates in one line
Ex- ‘hello world’

2.Multiline strings-That are made in multiple lines ,


can be created by using a \ at the end of line or by
typing the text in triple quotation marks’’’.
Ex- 1.‘hello\ 2.‘’’hello
world’ world’’’
Size of strings

Use len() to get the length or size of an object.


Examples
• ‘\\’ size is 1
• ‘abc’ size is 3
• ‘Seema\’s pen’ size is 11
Escape Sequence

An escape sequence represents a single character and hence


consumes one byte in ASCII* representation

ASCII-American Standard Code for Information


Interchange
ASCII
Alphabets a n d
Numbers
ASCII
Special
Characters
Unicode
For h i n d i alphabets
3.3.3B N u m e r i c
Literals
Are of three types
I. Integer Literals : are whole numbers without any
fractional part . It can be of three types :-
A. Decimal Integer literals-123,-67,90
B. Octal Integer literals-0o24,0o64
C. Hexadecimal Integer literals-0xC,0x12,0x19AF
Conversion Table
From decimal –octal –hexa decimal
2. Floating Point Literals : are also called real literals.
These may be written in one of the 2 forms called :-
A. Fractional Form : 2.0,17.5,-0.0098,.9,7.
B. Exponent Form : -0.9E9,0.9E-08,87E8

*Tuple – Is a sequence of values only.


3. Complex Literals – are of the form a+bJ , where a
and b are float and J (or j) represents -1^1/2 , which is
an imaginary number . a is the real part of the number
, and b is the imaginary part.
Ex- 2+7J , 4-4j , 25-69J etc.

We will learn more about complex numbers in the


following chapters.
3.3.3C B o o l e a n
Literals
A Boolean Literal in Python is used to represent one of
the two Boolean values i.e., True (Boolean True) or
False (Boolean False)

3.3.3D S p e c i a l L i t e r a l
N
Theo
Non
ne ve
alue in python means “There is no useful
information ” or “ There is nothing here”
3.3.4 O P E R A T O R S
Operators are tokens that trigger some computation /
action when applied to variables and objects in an
expression .

Unary operators-are those operators that require one


operand to operate upon : + - ~ not etc.

Binary operators-are those operators that require two


operands to operate upon
Arithmetic operators
□ Addition +
□ Subtraction -
□ Multiplication *
□ Division /
□ Remainder/Modulus %
□ Exponent **
□ Floor Division //
Bitwise operators
□ Bitwise AND &

□ Bitwise OR |

□ Bitwise exclusive OR ^
Identity operators

□ Is the identity same? is


□ Is the identity not same? is not
relational operators
□ Less than <
□ Greater than >
□ Less than or equal to <=
□ Greater than or equal to >=
□ Equal to ==
□ Not equal to !=
assignment operators
□ Assignment =
□ Assign quotient /=
□ Assign sum +=
□ Assign product *=
□ Assign remainder %=
□ Assign difference -=
□ Assign exponent *=
□ Assign floor division //=
Logical operators

□ Logical AND and


□ Logical OR or
Membership operators

□ Whether variable in sequence in


□ Whether variable not in sequence not in
PUNTUATORS

Puntuators are symbols that are used in


programming languages to organize
programming sentence structure , and indicate
the rhythm and emphasis of expression ,
statement , and programming structure
Examples : ‘ “ @ : ; {} [] () = etc.
3.4 B a r e b o n e s o f a
Python Program
A python program contains various component like
□ Expressions
□ Statement
□ Comments
□ Functions
□ Blocks and Indentation
EXPRESSIONS
An expression is any legal combination of symbols that
represent a value .
An expression represents something , which Python
evaluates and which then produces a value .
Ex. – 15 , 2.9 , a+5 , (3+5)/4
STATEMENT
A statement is a programming instruction that does
something i.e. , some action takes place . It is not
necessary that a statement result in a value ; it may or
may not yield a value.
Ex.- a=15 , b=a-9 , print(a+3) , if b<6 , etc.
COMMENTS
Comments are the additional readable information to
clarify the source code . Comments in Python begins
with symbol # and generally end with end of the
physical line .
Comments enclosed in triple quotes (“””) or triple
apostrophe (‘’’) are called Docstrings .
Ex.- # This is a sample comment ,
‘’’ This is a
sample
comment ‘’’
FUNCTIONS
A function is a code that has a name and it can be
reused ( executed again ) by specifying its name in the
program , where needed . There are two types of
functions :-
1. User defined functions
2. Built in functions
Ex- SeeYou() # User defined , print() , type() # Built in
etc.
Blocks a n d
Indentation
Sometimes a group of statements is a part of another
statement or function . Such a group of one or more
statements is called block or code block or suite.
For example
3.5 V a r i a b l e s a n d
Assignment
Named labels , whose values can be
used and processed during run , are
called variables .
variable
Python variables are created by assigning value of
desired type to them , e.g. , to create a numeric
variable , assign a numeric value to variable , assign a
string value to variable_name , and so on .
Ex-
“Jacob”
Student

Age 16
h a n d l i n g of
variables
Memory has literals/values at defined memory
locations , and each memory location has a memory
address.
You have given statement age=15 , variable age will be
created as a label pointing to memory location where
value 15 is stored.
And when you give statement age = 20 , then …..
Variables

14 15 16 17
20200 20216 20232 20248
Variables

14 15 16 17
20200 20216 20232 20248

Age now referring to location 20216 that has value 15


Variables

14 15 16 17
20200 20216 20232 20248

Now age is referring to location 20248


Lvalues a n d Rvalues

Lvalues are the objects to which you can assign a


value or expression . Lvalues can come on lhs or rhs of
an assignment statement .
Rvalues are the literals and expression that are
assigned to lvalues . Rvalues can come on rhs of an
assignment statement .
Assignments
In Python , assigning a value to a variable means ,
variable’s label is referring to that value .
1. Assigning same value to multiple variables
You can assign same value to multiple variables in a
single statement , e.g. ,
a=b=c=10
2. Assigning multiple values to multiple variables
You can even assign multiple values to multiple
variables in single variable , e.g. ,
x,y,z=10,20,30
3.5.3 V a r i a b l e D e f i n i t i o n
A variable is created when you first assign a value to it .
It also means that a variable is not created until some
values is assigned to it .
x=0 #variable x created now
print(x)
x=20
print(x)
3.5.4 D y n a m i c T y p i n g
A variable pointing to a value of second type , can be
made to point to a value /object of different type , this is
called DYNAMIC TYPING .

x Int : 10

x Int : 10

String : Hello world


Caution of dynamic typing
Although Python is comfortable with changing types of
a variable , the programmer is responsible for ensuring
right types for certain type of operations.
For Example:-
Output
The input() function always returns a value of String
type
Reading Numbers

Python offers two functions int() and float() to be used


with input() to convert the values received through
input() into int and float types .

After using int() and float () functions with input() , you


can check yourself using type() function that the type of
read value is now int or float now .
Possible errors w h e n
reading numeric
values
Consider the following program
• age=int(input(‘Enter your age :’))
•Percentage=float(input(‘Enter your percentage:’))

Then the possible errors are …..


Statement
The simplified syntax to use print() function is shown below :
Print(object, [sep=‘ ’ or <separator-string> end=‘\n’ or
<end-string>]).
A print() function without any value or expression or name
prints blank line.
In print() function , the default value of end argument is newline
character (‘\n’) and of sep argument ,it is space (‘ ’)

You might also like