logo Use SA10RAM to get 10%* Discount.
Order Now logo

Ask This Question To Be Solved By Our ExpertsGet A+ Grade Solution Guaranteed

expert
Avinash KumarData mining
(/5)

707 Answers

Hire Me
expert
Hariom KasyapFinance
(5/5)

732 Answers

Hire Me
expert
Jaydon KeySocial sciences
(5/5)

712 Answers

Hire Me
expert
Cristopher WebbTechnical writing
(5/5)

589 Answers

Hire Me
Python Programming
(5/5)

Write a Python program calculate the product, multiplying all the numbers of a given tuple.

INSTRUCTIONS TO CANDIDATES
ANSWER ALL QUESTIONS

Review Week 2 presentation notebooks

ists, tuples,sets, and dictionaries are containers for other data. They can be used to combine your data in more complex ways.

Data structure Properties Syntax

List Ordered, mutable sequence mylist = [1,2,3]

Tuple Ordered, immutable sequence mytuple = (1,2,3)

Set Unordered set of unique values set(1,2,3)

Dictionary Mutable set of key, value pairs mydict = {'first_value':1, 'second_value:2}

 

Logic operators

We test conditions using logic operators.

Symbol Task Performed

== True, if it is equal

!= True, if not equal to

< less than

<= less than or equal to

> greater than

>= greater than or equal to

# NOTE: We declare variables using '=' a = 5

b = 7

# But compare them using '==' a == b

False

 

 

# Test whether a does not equal b a != b

 

True

 

# Logic expressions evaluate to True or False (datatype: Bool) test = b > a

print(test)

print(type(test))

 

True

<class 'bool'>

 

 

Testing conditions inside a loop

Combining loops with logic allows you to build more sophisticated code structures:

 

 

days = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun']

 

for day in days:

if day == 'Sat' or day == 'Fri': location = 'TSI'

elif day == 'Sun':

location = 'Home!' else:

location = 'Work + TSI' print(day, location)

 

Mon Work + TSI Tue Work + TSI Wed Work + TSI Thu Work + TSI Fri TSI

 

Sat TSI

Sun Home!

 

authorized_pets = ['small dog', 'cat', 'hamster','budgerigar']

 

print("Welcome to Nick's Apartment Block.")

my_pet = input("Type your pet's breed to see if it's accepted: ")

 

if my_pet in authorized_pets:

print(f"Congratulations, your {my_pet} is welcome here!") else:

print(f"Sorry your {my_pet} is NOT ACCEPTED")

 

Welcome to Nick's Apartment Block.

Type your pet's breed to see if it's accepted: dog Sorry your dog is NOT ACCEPTED

 

 

Exercises

 

Please organise your solutions as a separate notebook and upload to the LMS (Moodle):

Notebook (.ipynb)

Generate pdf (File -> Download as -> PDF).

 

 

EXERCISE 1

Review operations on lists (adding/removing elements, slicing, sorting, etc.). Illustrate their usage on a list of your choice.

 

 

# YOUR CODE HERE:

my_order_listODD = [4, 5, 6, 7, 8, 9, 11, 13, 15, 16] my_order_listODD.append(17)

my_order_listODD.remove(8)

#Removing all even numbers for ele in my_order_listODD:

if ele % 2 == 0:

my_order_listODD.remove(ele)

 

my_order_listODD.sort() print(my_order_listODD)

 

my_listcut_v1 = my_order_listODD[1:-3] print(my_listcut_v1)

 

my_listcut_v2 = my_order_listODD[::4] print(my_listcut_v2)

 

[5, 7, 9, 11, 13, 15, 17]

[7, 9, 11]

[5, 13]

 

 

EXERCISE 2

Review operations on sets (like intersection or pop) using the table https://wesmckinney.com/book/python-builtin.html#tbl- table_set_operations and illustrate usage of at least 3 of them for a set of your choice.

 

 

# YOUR CODE HERE:

my_data_set = {'data1', 'data2', "data3", "data4", "data5", "data6"}

 

 

my_data_set.add("data10")

 

 

my_data_set.pop() print(my_data_set)

 

my_value_set2 = {'value3', 'value5', 'value7', 'value9'}

 

value_data_inter = my_data_set.intersection(my_value_set2) print(value_data_inter)

 

value_data_uni = my_data_set.union(my_value_set2)

 

print(value_data_uni)

 

 

{'data4', 'data2', 'data5', 'data10', 'data6', 'data3'} set()

{'value5', 'value7', 'value3', 'value9', 'data4', 'data2', 'data5', 'data10', 'data6', 'data3'}

 

 

 

 

 

 

 

# YOUR CODE HERE:

fruits = ("apple", "orange", "pear") veg = "broccoli"

fruits_and_veg = str(fruits) + veg print(fruits_and_veg)

 

fruits_and_veg2 = fruits+(veg,) print(fruits_and_veg2)

 

('apple', 'orange', 'pear')broccoli

('apple', 'orange', 'pear', 'broccoli')

 

 

EXERCISE 4

Write a Python program to a string of comma-separated values into a tuple

 

 

# YOUR CODE HERE:

fruit_string = "apple, orange, pear"

 

fruit_tuple = tuple(fruit_string.split(',')) print(fruit_string)

print(fruit_tuple)

 

apple, orange, pear

('apple', ' orange', ' pear')

 

 

EXERCISE 5

Write a Python program to check if all values in a list are different

 

 

# YOUR CODE HERE:

even_list = [2, 4, 6, 8, 10, 12, 2, 4, 6]

 

if (len(set(even_list)) == len(even_list)): print("All elements are unique")

 

else:

print("All elements are not unique")

 

 

All elements are not unique

 

 

EXERCISE 6

Write a Python program calculate the product, multiplying all the numbers of a given tuple. Original Tuple: (4, 3, 2, 2, -1, 18) Product: -864

Original Tuple: (2, 4, 8, 8, 3, 2, 9) Product: 27648

Original Tuple: (1, "Two", 3, "Four", 5) Product: 15

 

 

# YOUR CODE HERE:

def mutiple_tuple(nums): temp = list(nums)

product = 1

for x in temp:

if not isinstance(x,int): continue product = product * x

return product

 

nums = (4, 3, 2, 2, -1, 18)

print(mutiple_tuple(nums))

 

import numpy

original_tuple2 = (2, 4, 8, 8, 3, 2, 9) result1 = numpy.prod(original_tuple2) print(result1)

 

nums= (1, "Two", 3, "Four", 5) print(mutiple_tuple(nums))

 

 

-864

27648

15

 

(5/5)
Attachments:

Related Questions

. The fundamental operations of create, read, update, and delete (CRUD) in either Python or Java

CS 340 Milestone One Guidelines and Rubric  Overview: For this assignment, you will implement the fundamental operations of create, read, update,

. Develop a program to emulate a purchase transaction at a retail store. This  program will have two classes, a LineItem class and a Transaction class

Retail Transaction Programming Project  Project Requirements:  Develop a program to emulate a purchase transaction at a retail store. This

. The following program contains five errors. Identify the errors and fix them

7COM1028   Secure Systems Programming   Referral Coursework: Secure

. Accepts the following from a user: Item Name Item Quantity Item Price Allows the user to create a file to store the sales receipt contents

Create a GUI program that:Accepts the following from a user:Item NameItem QuantityItem PriceAllows the user to create a file to store the sales receip

. The final project will encompass developing a web service using a software stack and implementing an industry-standard interface. Regardless of whether you choose to pursue application development goals as a pure developer or as a software engineer

CS 340 Final Project Guidelines and Rubric  Overview The final project will encompass developing a web service using a software stack and impleme

Get Free Quote!

390 Experts Online