0% found this document useful (0 votes)
17 views18 pages

Data Structures

Uploaded by

Suguna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views18 pages

Data Structures

Uploaded by

Suguna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

DATA STRUCTURES

There are two types of data structures in python namely:

1.Built In Data structures.

2.Userdefined Data Structures.

Built In Data Structures are:

 Numbers
 Strings
 Lists
 Tuples
 Dictionary
 Boolean
 Sets

User Defined Data Structures are :

 Stack
 Tree
 Graph
 Queue
 Linked List
 Pie Chart.

NUMBERS

Number data types store numeric values. They are immutable data types, means that changing the
value of a number data type results in a newly allocated object.

Types: Integers

Float

Integers:
Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.

Whole numbers- 1,2,3 etc…

No decimals are allowed.

Int is a built in function in python.


FLOAT :
 Float is is a data type composed of a number that is not an integer, because it includes a
fraction represented in decimal format.
 0.4,0.8 etc…

 Can display digits past the decimal point

Arithmetic Operators:

 Addition +
 Subtraction –
 Multiplication *
 Division /
 Floor Division //
 Percentage %
 Power **

Operator Description Example

+ Addition Adds values on either side of the operator. a+b=


30

- Subtraction Subtracts right hand operand from left hand operand. a–b=-
10

* Multiplies values on either side of the operator a*b=


Multiplication 200

/ Division Divides left hand operand by right hand operand b/a=2

% Modulus Divides left hand operand by right hand operand and returns b%a=
remainder 0

** Exponent Performs exponential (power) calculation on operators a**b =10


to the
power 20
// Floor Division - The division of operands where the result is 9//2 = 4
the quotient in which the digits after the decimal point are and
removed. But if one of the operands is negative, the result is 9.0//2.0
floored, i.e., rounded away from zero (towards negative = 4.0, -
infinity) − 11//3 = -
4, -
11.0//3 =
-4.0

Sample Programs using Arithmetic Operators:

Program 1:

x = 15
y=4
# Output: x + y = 19
print('x + y =',x+y)

# Output: x - y = 11
print('x - y =',x-y)

# Output: x * y = 60
print('x * y =',x*y)

# Output: x / y = 3.75
print('x / y =',x/y)

# Output: x // y = 3
print('x // y =',x//y)

# Output: x ** y = 50625
print('x ** y =',x**y)

Program 2 :

Arithmetic Operators using Functions:

Input:

import sys

def main():

num1 = int(input("Enter the Integer Number1:"))


num2 = int(input("Enter the Integer Number2:"))

print()

print("Addition of two number :", add(num1,num2))

print("Subtraction of two number :",subtract(num1,num2))

print("Multiplication of two number :",multiply(num1,num2))

print("Division of two number :",divide(num1,num2))

def add(num1,num2):

result = num1+num2

return result

def subtract(num1,num2):

result = num1-num2

return result

def multiply(num1,num2):

result = num1*num2

return result

def divide(num1,num2):

if(num2==0):

print("error")

result = num1//num2

return result

if __name__ == "__main__":

main()

Output:

Enter the Integer Number1:13

Enter the Integer Number2:34

Addition of two number : 47

Subtraction of two number : -21

Multiplication of two number : 442

Division of two number : 0


Comparison Operators:
Mainly generate Boolean value true or false.

Also called as relational operators

The types of comparison operators are:

 Equal to ==
 Not Equal to !=
 Greater than >
 Less than <
 Greater than or equal to >=
 Less than or equal to <=

Operator Description Example


Decides whether the values are equal a = 20, b = 20 a
Equal-To (== )
or not. == b returns True
Not-Equal-To (!= Decides whether the values are a = 10, b = 12a !=
) unequal or not. b returns True
Greater-Than Decides whether the left-value is a = 25, b = 10 a >
(> ) greater or not. b returns True
Decides whether the left-value if lesser a = 10, b = 25 a <
Less-Than (< )
or not. b returns True
Decides whether the left-value is
Greater-Than- a = 10, b = 10 a
greater or equal to the right value or
Equal-To (>= ) >= b returns True
not.
Less-Than-Equal- Decides whether the left-value is lesser a = 34, b = 34 a
To (<= ) or equal to the right value or not. <= b returns True

Example Programs:
Program 1:
a=12
b=8
print(a == b) . Output : False
print(a != b). Output : True
print(a>b) . Output: True
print(a<b). Output: False
print(a>=b) Output: True
print(a<=b). Output : False

Assignment Operators:
They are mainly used to assign values to the variables.

Operator Description Syntax

Assign value of right side of expression to left side x=y+


=
operand z

+=
Add and Assign: Add right side operand with left side
operand and then assign to left operand a += b

Subtract AND: Subtract right operand from left operand


and then assign to left operand: True if both operands
-=
are equal a -= b

Multiply AND: Multiply right operand with left operand a *= b


*=
and then assign to left operand

Divide AND: Divide left operand with right operand and


/=
then assign to left operand a /= b

Modulus AND: Takes modulus using left and right


%=
operands and assign result to left operand a %= b

Divide(floor) AND: Divide left operand with right


//=
operand and then assign the value(floor) to left operand a //= b

Exponent AND: Calculate exponent(raise power) value a **= b


**=
using operands and assign value to left operand

Performs Bitwise AND on operands and assign value to


&=
left operand a &= b
Operator Description Syntax

|=
Performs Bitwise OR on operands and assign value to
left operand a |= b

Performs Bitwise xOR on operands and assign value to


^=
left operand a ^= b

Performs Bitwise right shift on operands and assign


>>=
value to left operand a >>= b

a<<=b.
Performs Bitwise left shift on operands and assign value
<<=
to left operand.
Sample Programs:

Program 1:

a=3

b=5

c=a+b

# Output

print(c)

Output: 8

Program 2:

a=3

b=5

#a=a-b

a -= b

# Output

print(a)

Output: -2
Program 3:

a=3

b=5

#a=a*b

a *= b

# Output

print(a)

Output: 15

Program 4:

a=3

b=5

# a = a ** b

a **= b

# Output

print(a)

Output: 243

Program 5:
a=17
b=10
print("Binary number for {0} is {0:08b}".format(a))
print("Binary number for {0} is {0:08b}".format(b))

Output:

Binary number for 17 is 00010001

Binary number for 10 is 00001010


Program 6: (And if both bits are 1):

a = 17

b = 10

#a=a&b

a &= b

# Output

print(a)

Output : 0

Explanation: 00010001 (a=17)

00001010(b=10)

00000000(comparing both)

Program 7:
a = 17
b = 10
# a = a | b
a |= b
# Output
print(a)

Output: 27

Output Explanation: 00010001 (a=17)

00001010(b=10)

00011011(comparing both)

Program 8:
a = 17
b = 10
# a = a ^= b
a ^= b
# Output
print(a)

Output : 27
Output Explanation: 00010001 (a=17)

00001010(b=10)

00011011(comparing both)

Program 9:

a=3

b=5

# a = a >> b

a >>= b

# Output

print(a)

Output : 0

Logical operators:

 And
 OR
 NOT

AND:

If both are true it is true or else false

T T –T

TF–F

FF–F

OR:

If either one is it is true

T,F – T

T,T –T

F,F –F
Not:

It works like negotiation.

a=4>5

not a

Output -- True

Program:

a= 1>3

b=8>7

a and b

Output: False

a or b

Output: True

Other Built in Functions in python:

abs:

print(abs(-4))

output : 4

round:

print(round(3.55,1))

output : 3.6

type:

print(type(a))

output: integer
all() :

It returns true if all items passed in iterable are true or else it returns false.
k=[1,3,4,6]

Print(all(k)) .Output: True

K=[1,3,7,0]

Print(all(K)). Output : False

bin():

It is used to return binary representation of a specified integer. A result always starts with prefix 0b

x=10

y=bin(x)

print(y)

Output: 0b1010

Float():

Print(float(9))

Output : 9.0

Print(float(8.9))

Output: 8.9

Len():

Name= “Vamsi”

Print(len(Name))

Output: 5

IF Else Statement in Python:

 It evaluates whether an expression is true or false.


 If a condition is true, if statement executes otherwise else statement executes.
 These statements helps coders to control the flow of programs.
Programs:

Program 1: even or odd number


num = int(input("Enter a number: "))
mod = num % 2
if mod > 0:
print("This is an odd number.")
else:
print("This is an even number.")

Output:

Enter a number: 77

This is an odd number.

Program 2:
num=5
if num==1:
print('Monday');
elif num==2:
print('Tuesday')
elif num==3:
print('Wednesday')
elif num==4:
print('Thursday')
elif num==5:
print('Friday')
elif num==6:
print('Saturday')
elif num==7:
print('Sunday')
else:
print('Invalid input, please enter 1 to 7 only')
print('Learn Python with Vamsi')

Output: Friday

Learn Python with Vamsi

STRINGS
 They are sequence of characters with single or double quotes. Example- ‘India’, “Vamsi”.
 They are ordered sequences where we can perform indexing and slicing.
Vamsi
012 34
Print(a[2])
Output: 2

Basic Programs:

Program 1:

Name = “Vamsi”

Print(Name)

Output: Vamsi

Program 2 : Concatenation( adding strings)

Name = “Vamsi”

Surname = “ Eluri”

Print(Name + Surname)

Output: VamsiEluri

Program 3 : Replication:

Name = “Vamsi”

Print(Name*5)

Output: VamsiVamsiVamsiVamsiVamsi

Program 4: Slice Operator( To print a part of a string)

Name = “Vamsi”

Print(Name[1:3])

Output: am

Print(Name[2:])

Output: msi

Print([-3:-1])

Output: ms

Name = “Vamsi Chowdary”

Print(name[::4])

Output: Vior
Program 5: Capitalization

Name = “vamsi”

print(Name.capitalize())

Output: Vamsi

Program 6: Count method

name = “VamsiKrishna”

substring = “a”

print(name.count(substring))

Output : 2

Program 7: Index

name= “Vamsi”

sub = “ms”

print(name.index(sub))

Output: 2

Program 8: alphanumeric

name = “Vamsi”

print(name.isalnum())

output: True

name = “V@amsi”

print(name.isalnum())

Output: False
Program 9: alphabets

name = “Vamsi”

print(name.isalpha())

Output: True

name = “Vamsi Krishna”

Print(name.isalpha())

Output: False

Program 10: Numbers

a = “V12”

print(a.isdigit())

Output: False

Program 11: Upper or Lower case

name = “VAMSI”

print(name.islower())

print(name.isupper())

Output: False

True

Program 12: To print in Lower or upper case

name = “VAMSI”

print(name.lower())

Output : Vamsi

name= “Vamsi”

print(name.lower())
Output: Vamsi

name = “VAmsI”

print(name.upper())

Output: VAMSI

Program 13: SWAP

name = “VaMSi”

print(name.swapcase())

Output:vAmsI

You might also like