You are on page 1of 100

Features of python

 Interpreted
 Object oriented programming language
 Easy & simple
 Portable

Two modes to use python

 Interactive mode
 Script mode

Python character set

It is a set of valid characters that a language recognize.


 Letters: A-Z, a-z
 Digits: 0-9
 Special symbols
 White space

Tokens

Token: smallest individual unit in a program is known as token

There are five types of tokens in python:

1. Keywords
2. Identifier
3. Literal
4. Operators
5. Punctuators

Keywords

Keyword: Reserved word in the library of a language. There are 33 keywords in python

Identifier

1. Identifier: the name given by the user to the entities like variable name, class name ,function
name etc

Rules for identifiers:


It can be combination of upper case or lower case
_ is the only special symbol that can be used
Numeric literals
Escape secret character

How many ‘ before the string that much must be there

Boolean literal

True and false

Special literal

None

Literal collections

Delimiters( learn only the definitions)

A delimters is a sequence of one or more character used to specify the boundary between
separate , Independent regions in plain txt or other data streams An example of a delimiter is the
comma character , which acts as a field delimiter in a sequence of comma separated values

Binary operators

Steps for problem solving using a computer

Types of testing

Unit testing

Integration testing

System testing

Acceptance testing

Developing an algorithm

A set of exact steps which when followed solve a problem or accomplish the task

Characteristics of an algorithm

Precision -the steps are precisely stated or defined

Uniqueness-results of each step are uniquely defined and only depends on the in put and the result
of the preceding steps

Finiteness- the algorithm always stops after a finite number of steps

Input-the algorithm receives some input


Output-the algorithm produces some input

While writing an algorithm the following are clearly identified and required

Every algorithm at least must have three statement input ,process, output

Representation of algorithms

There are two common methods

1 flowchart

2 pseudocode

1 flowchart- is a visual representation of an algorithm

Flow chart is a diagram made up of boxes diamonds and other shapes connected by arrows

Flowchart symbol function


Flowchart to calculate the square of a number

Start

Input num

Square=num*num

Print output

stop

pseudocode

it is an other way of representing an algorithm

it is not a real code

it is whatever you under stand

it is a non formal language

increment mean increase

decrement mean decrease

frequently used keywords while writing pseudocode are

input

compute
print

increment

decrement

if/else

while

true/false

benefits of pseudocode

it helps in representing basic functions of high-level programming language

humans can understand it but computers can’t understand it

Write an algorithm to find area and length of a rectangle

Start

Input L

Input B

Compute P=2*(L+B)

Print P

Compute A=l*b
Print A

Stop

Flowchart :

Start

Input L and B

P=2*(L+B)

A=L*B

Print P, A

Stop

Decomposition

Definition: Breaking down larger task into smaller tasks

Examples

Railway reservation system in India has the following sub module

Flow control

Sequence: there is no decision

Selection: there is some conditions here

Looping or repetition:

Mutable

The Values that can be changed

Immutable

The Values that can be changed

Expression

It is a combination of constants, variable and operators

a stand-alone operator cannot be an expression

operators

it is used to perform mathematical operations on values

types of operators

Arithmetic operators

Relational operators
Assignment operators

Logical operators

Identify operators

Membership operators

Arithmetic operators

+ add

- subtract
* multiplication

/ division

% modulus ( it gives the remainder ) ( if a% n is there where n is greater than a the result will
always be a)

// floor division ( it always rounds down)

* * exponent ( its basically 2**3=2^3)

Relational operators

It compare the values (number)

A=65 (ascii)
a=97(ascii)

Assignment operators

Change the value of left

E.g.

X+=2

X=x+2

Y=y**3

Y**=3

Logical operators
v

identity operator
Membership operators
~x=-x-1
Debugging The process of identifying and removing mistakes

Syntax error the rulers are not followed then it shows syntax error

Logical error we will get a answer but not the expected answer
Runtime error it will work it will give right output but in will stop working if we give wrong input

Type conversion changing the data type

Explicit

We will specify to the system to covert the data type to another


Implicit conversation it will change its self to another
``

Nested loop

Outer for loopz

Inner for loop


Pyramid
Concatenation is combing two strings
Endswith()

str="my name is"

print(str.endswith('is',-1,-5))

isalnu

str="my name is"

print(str.isalnum()

import function

from math import pi

pi*r

or

import math

math.pi*r
Is alpha

Functions
Return and parameter are optional
def fact(x,y):
sum=x+y
return(sum)

x=float(input('Enter your first number'))


y=float(input('Enter your second number'))
ans=fact(x,y)
print(ans)
List
Any data types can be there in list

A list within a list is possible (nested list)


One list is considered as one index
Eg
list1=[11,22,['surya',80],'m']
print(list1[2][0])
concatenation is adding to list (symbol + )
It does not check nested list

for k in list:
print(k)
1
['sci', 760]
2.0
l2.insert(0(#insrt the index here),700)
count returns the no of times the elements is in the list
eg l3=[100,200,300,400,500,100,100]
print(l3.count(100))
Another program code:
COUNT IS FOR FINDING THE NO of times the a letter is repeated
ANS
SCORES=[456,500,350,658,200,155]
sum=0
for i in SCORES:
if i%10==0:
sum+=i
print(sum)
SCORES.append(248)
>>> print(SCORES)
[456.5, 350, 658, 200, 155, 248]
SCORES.extend([1,2,3,4])
>>> print(SCORES)
[456, 500, 350, 658, 200, 155, 1, 2, 3, 4]
SCORES.insert(0,9)
>>> print(SCORES)
[9, 456, 500, 350, 658, 200, 155, 1, 2, 3, 4]

Remove() we have to specify the element


Sort() will change the orginal list
Sorted() will make new list
Syntax for function :
min(l3)
max(l3)
sum()
remove will work with mixed data types
pop also

r everse also
\
Len(tuple name)
b.count(the no)
b.index(the tuple component)
sorted(b)
min(b)
max(b)
sum(b)
t=tuple()
n=int(input('Enter thr no of elements:'))
print('Enter the elements')
for i in range (n):
num=int(input())
t=t+(num,)
print('the tuple is:',t)
print('the max of the tuple is',max(t))
print('the min of the tuple is',min(t))

d={1:'10',2:'20'}
>>> d[3]='30'
>>> print(d)
{1: '10', 2: '20', 3: '30'}
Searching is based on key
len(f)
f.keys()
f.values()
f.items()
f.get(1)
f.update({1:100}) it also add key-value
-
Get function gets function of the key
Syntax of get is d.get()
It works for tupil but not list
The key cant be a llist

Syntax for del is del(d[1])


sorted(d2.items()) (syntax)
[(5, '50'), (6, '60'), (8, '80'), (9, '90')]
>>> sorted(d2.values()) (syntax)
['50', '60', '80', '90']
Del deletes the entire dict
Where as clear only the elements inside the elements
Update combines the two dict
Copy copies the elements into the new dict
Pop removes the key
d2.pop(10,'not in d2')
'not in d2'
d2={10:'ten',20:"twenty",30:"thirty"}
>>> print(min(d2))
10
>>> print(sum(d2))
60
>>> print(max(d2))
30
or i in range(n):
m=int(input('roll'))
f=input('marks')
d1[m]=f
print(d1)
Logic gates:
Ic (integrated circuit )
It will take many inputs and give one output
3 basic gates:
And
Or
Not
1) And:
Xor gate
The xoc or gate will take two or more In put and produce one output when it has odd no
of 1 in input combination then the output will be 1 if it has even no of 1 then the output
will be 0 .
Xnor gate
Complement of xor gate
Basic thermos of algebra

Properties of zero and one


\
Absorption law
1) (A’B’+CD)’
2) (XY)’+Z
3) ((X+Y)’Z)’=(X+Y)’+Z’(De morgens law)
4) (((X+Y)’+Z)’+Y)’=((x’.y’)+z).y=(x’z+y’z).y
Number system

1) binary
2) decimal
3) octal
4) hexadecimal(0-15)
Revision
When use tuple in prog the the word is split
T6=tuple('hello')
>>> print(T6)
('h', 'e', 'l', 'l', 'o')

You might also like