Python Data Types Jayshree Aher Problem Statement Write
Python: Data Types Jayshree Aher
Problem Statement: Write a Python Program to check if a Number is a Strong Number. Objectives: 1. To learn about the python programming using control structure. 2. To understand the concept of interpreted language working. 9/12/2021 2
Python Data Types 1. 2. 3. 4. 5. Numbers String List Tuple Dictionary Ø Python supports four different numerical types − 1. 2. 3. 4. 9/12/2021 int (signed integers) long (long integers, they can also be represented in octal and hexadecimal) float (floating point real values) complex (complex numbers) 3
Python Data Types : List ØLists are the most versatile of Python's compound data types. ØA list contains items separated by commas and enclosed within square brackets ([]) all the items belonging to a list can be of different data type. List Subject = [ ] Subject = [ ‘PPL', ‘DELD’ , ‘OOP’, ‘DS’, ‘COA’] Index Code = [123, 124, 125, 126, 127] Negative index print (Subject) # Prints complete list print (Subject[0]) # Prints first element of the list print (Subject[1: 3]) # Prints elements starting from 2 nd till 3 rd print (Subject[2: ]) # Prints elements starting from 3 rd element print (Code * 2) # Prints list two times print (code[: ]) print (Subject + Code ) # Prints concatenated lists 9/12/2021 PPL DELD OOP DS COA 0 -5 1 -4 2 -3 3 -2 4 -1 4
Python Data Types : List Subject = [ 'PPL', 'DELD' , 'OOP', 'COA', 'DS', 'SPORTS' ] Code = [123, 156, 178, 189, 809, 155] print (Subject) print (Subject[0]) print (Subject[1: 3]) print (Subject[2: ]) print (Code * 2) print (Code[: ]) print (Code[: 2]) print (Code[6]) print (Code[-2]) Code[4]=234 print(Code) print (Subject + Code) 9/12/2021 5
Python Data Types : List print ("OOP is present at ", Subject. index("OOP")+1, "location of the list") print (Code. index("100")) # value error as 100 is string print (155 in Code) Subject. insert(0, "SYBTech") print (Subject) Subject. append(["CCA", "LCA"]) print (Subject) print("Length of list subject", len(Subject)) Subject. extend(["sub 11", "sub 22", "sub 33"]) print (Subject) print("Length of list subject", len(Subject)) Subject. remove("sub 11") print (Subject. pop()) Subject = Subject + ["sub 55"] print (Subject) 9/12/2021 6
Python Data Types : List # Find the sum of ‘n’ numbers n = input("Enter the number of elements to be calculated for finding the sum : ") n = int (n) sum = 0 for num in range(0, n+1, 1): sum = sum + num print("SUM of first ", n, "numbers is: ", sum ) 9/12/2021 7
Python Data Types : List #Find the average of elements of an array n = int(input("enter the size of array : ")) arr = [ ] sum=0 for i in range(n): x = int(input("enter element: ")) arr. append(x) sum=sum+arr[i] print(“Entered array is : ", arr, " and Average of elements is ", float(round((sum/n), 2))) 9/12/2021 8
Python Data Types: Tuples ØA tuple is another sequence data type that is similar to the list. ØThe main differences between lists and tuples are: Lists are enclosed in brackets ( [ ] ) and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be updated. Tup. Sub = ( ‘DMTA', ‘NLP’ , ‘CS 121’ ) Tup. Mob = (‘Iphone 6’, ‘Sony‘, ’Appo’) print (Tup. Sub) # Prints complete list print (Tup. Sub[0]) # Prints first element of the list print (Tup. Sub[1: 3]) # Prints elements starting from 2 nd till 3 rd print (Tup. Sub[2: ]) # Prints elements starting from 3 rd element print (Tup. Mob * 2 ) # Prints list two times print (Tup. Sub + Tup. Mob) # Prints concatenated lists 9/12/2021 9
Immutable and Mutable Tup. Sub = ( ‘DMTA', ‘NLP’ , ‘CS 121’ ) Code = [123, 124, 125] Tup. Sub[2] = ‘Compiler’ # Invalid syntax with tuple Code[2] = 1000 9/12/2021 # Valid syntax with list 10
Python Data Types: Dictionary ØPython's dictionaries are kind of hash table type. They work like associative arrays or hashes found in Perl and consist of key-value pairs ØDictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces ([]) Ex. course ={ ‘PPL' : ’Jayshree', ‘DS' : ’Priyanka‘, ‘COA’: ’Shamla’} print(course) 9/12/2021 11
Python Data Types: Dictionary course ={'PPL': 'Jayshree', 'DS': 'Priyanka', 'COA': 'Shamla'} print("dictionary : ", course) print(course. values()) print(course. keys()) print(course['DS']) # acess an element course["OOP"]="Shreya" print("dictionary : ", course) print("length dictionary : ", len(course)) course["OOP"]="Object Prog" # change value print("dictionary : ", course) del course["OOP"] print("dictionary : ", course) 9/12/2021 12
Strong Number Def: Strong number is a special number whose sum of factorial of digits is equal to the original number. Ex. Num: 145 Sum of digit factorials = 1! + 4! + 5! = 1 + 24 + 120 = 145 9/12/2021 13
Algorithm Problem Solution: Step - 1. Start & Accept the number and store it in a variable. Step - 2. Separate the digits of the number by using mod function (remainder). Step - 3. Find the factorial of each digit. Step - 4. Calculate the total sum of all the factorials of the digits. Step - 5. Check if the sum of the factorials of the digits is equal to the original number. Step - 6. Print the final result. Step - 7. Stop. 9/12/2021 14
Sample Program n = int(input("Enter a Number : ")) # accept the number num, sum 1 = n, 0 while n > 0: r = n % 10 # separating the digits (remainder) fact = 1 i=1 while i <= r: fact = fact * I # factorial of a number i=i+1 print("Factorial of ", r, " : ", fact) sum 1 = sum 1 + fact print("Sum is : ", sum 1), n = int(n/10) print("n. Digit : ", n) if sum 1 == num: print("n. The entered number", num, " is a Strong Number !") else: print("n. The entered number", num, "-> is NOT a Strong Number. . . ") 9/12/2021 15
Output runfile('C: /Users/DELL/Codes/strong_num. py', wdir='C: /Users/DELL/Codes') //--- Strong number program ----// Enter a Number : 145 Factorial of 5 : 120 Sum is : 120 Digit : 14 Factorial of 4 : 24 Sum is : 144 Digit : 1 Factorial of 1 : 1 Sum is : 145 Digit : 0 The entered number 145 -> is a Strong Number ! 9/12/2021 16
FAQs 1. What are the key features of Python? 2. Is indentation optional in Python? Justify your answer. 3. What are the built-in types available in python? 4. What is a string in python? 5. State the difference between a list and a tuple. 6. Justify the difference between the append() and extend() functions for list operations. 7. Compare the mutable and immutable objects in python. 9/12/2021 17
Practice Assignments 1. Python Program to Check Whether a Number is Positive or Negative. 2. Python Program to Check if a Number is a Palindrome. 3. Python Program to Check if a Number is a Perfect Number. 4. Python Program to Check if a Number is a Prime Number. 5. Python Program to Find the Sum of the Digits of the Number. 6. Python Program to Find the Factorial of the Number. 9/12/2021 18
References Mark Lutz, Learning Python, 5 th Edition, O'Reilly Media, Inc. ISBN: 9781449355739 https: //nptel. ac. in https: //w 3 schools. com www. sanfoundry. com Thank you ! 9/12/2021 19
- Slides: 19