- To import an individual function or class from a module:
from module_name import object_name - To import multiple individual objects from a module:
from module_name import first_object, second_object - To rename a module:
import module_name as new_name - To import an object from a module and rename it:
from module_name import object_name as new_name - To import every object from a module
import module_name
Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts
Monday, May 04, 2020
Python - Import
Python Examples - ListComprehensions, zip and Enumeration
List Comprehensions:
1. Find students scored more than 60 using
scores = {
"Rick Sanchez": 70,
"Morty Smith": 35,
"Summer Smith": 82,
"Jerry Smith": 23,
"Beth Smith": 98
}
passed = [name for name, score in scores.items() if score >= 60]
print(passed)
2. Print multiples of 3
multiples_3 = [x * 3 for x in range(1, 21)]
print(multiples_3)
Zip
1. Merge below lists to print in the format - F:23,677,4
x_coord = [23, 53, 2, -12, 95, 103, 14, -5]
y_coord = [677, 233, 405, 433, 905, 376, 432, 445]
z_coord = [4, 16, -6, -42, 3, -6, 23, -1]
labels = ["F", "J", "A", "Q", "Y", "B", "W", "X"]
points = []
for point in zip(labels, x_coord, y_coord, z_coord):
points.append("{}: {}, {}, {}".format(*point))
for point in points:
print(point)
2. Zip lists to dictionary
cast_names = ["Barney", "Robin", "Ted", "Lily", "Marshall"]
cast_heights = [72, 68, 72, 66, 76]
cast = dict(zip(cast_names, cast_heights))
print(cast)
Enumeration
cast = ["Barney Stinson", "Robin Scherbatsky", "Ted Mosby", "Lily Aldrin", "Marshall Eriksen"]
heights = [72, 68, 72, 66, 76]
for i, character in enumerate(cast):
cast[i] = character + " " + str(heights[i])
print(cast)
Dictionary
result = 0
basket_items = {'apples': 4, 'oranges': 19, 'kites': 3, 'sandwiches': 8}
fruits = ['apples', 'oranges', 'pears', 'peaches', 'grapes', 'bananas']
for object, count in basket_items.items():
if object in fruits:
result += count
print("There are {} fruits in the basket.".format(result))
1. Find students scored more than 60 using
scores = {
"Rick Sanchez": 70,
"Morty Smith": 35,
"Summer Smith": 82,
"Jerry Smith": 23,
"Beth Smith": 98
}
passed = [name for name, score in scores.items() if score >= 60]
print(passed)
2. Print multiples of 3
multiples_3 = [x * 3 for x in range(1, 21)]
print(multiples_3)
Zip
1. Merge below lists to print in the format - F:23,677,4
x_coord = [23, 53, 2, -12, 95, 103, 14, -5]
y_coord = [677, 233, 405, 433, 905, 376, 432, 445]
z_coord = [4, 16, -6, -42, 3, -6, 23, -1]
labels = ["F", "J", "A", "Q", "Y", "B", "W", "X"]
points = []
for point in zip(labels, x_coord, y_coord, z_coord):
points.append("{}: {}, {}, {}".format(*point))
for point in points:
print(point)
2. Zip lists to dictionary
cast_names = ["Barney", "Robin", "Ted", "Lily", "Marshall"]
cast_heights = [72, 68, 72, 66, 76]
cast = dict(zip(cast_names, cast_heights))
print(cast)
Enumeration
cast = ["Barney Stinson", "Robin Scherbatsky", "Ted Mosby", "Lily Aldrin", "Marshall Eriksen"]
heights = [72, 68, 72, 66, 76]
for i, character in enumerate(cast):
cast[i] = character + " " + str(heights[i])
print(cast)
Dictionary
1. Count Fruits
basket_items = {'apples': 4, 'oranges': 19, 'kites': 3, 'sandwiches': 8}
fruits = ['apples', 'oranges', 'pears', 'peaches', 'grapes', 'bananas']
for object, count in basket_items.items():
if object in fruits:
result += count
print("There are {} fruits in the basket.".format(result))
Sunday, May 03, 2020
Python - Boolean Check
Any constant can be represented as a boolean value False if its value is zero of any numeric type or empty as defined below.
eg:
if test:
print("Success")
else:
print("Failure")
This prints Success if test is not empty and any value greater than 0 (eg: test=4) and false otherwise
Subscribe to:
Posts (Atom)