Python for Experienced Java Developers Quotes

Rate this book
Clear rating
Python for Experienced Java Developers Python for Experienced Java Developers by Jörg Richter
1 rating, 5.00 average rating, 1 review
Open Preview
Python for Experienced Java Developers Quotes Showing 61-90 of 144
“In case an exception (Exceptions will be discussed in Chapter 7: Exceptions.) occurs during the execution of the with block, the exc_type, exc_value, and traceback parameters hold the class of the exception, the exception itself, and the exception’s traceback attribute, respectively.”
Jörg Richter, Python for Experienced Java Developers
“You can create a custom context manager by defining __enter__ and __exit__ methods in a class.”
Jörg Richter, Python for Experienced Java Developers
“Context managers in Python streamline resource management and controlled setup and cleanup actions. They are typically used with the with statement, which is similar to Java’s try-with-resources statement.”
Jörg Richter, Python for Experienced Java Developers
“Generator functions are regular functions that use a yield statement to provide the next element in the sequence. def my_iterator(limit):
for i in range(1, limit+1):
yield i


for x in my_iterator(4): # 1 2 3 4
print(x)

print(list(my_iterator(4))) # [1, 2, 3, 4]”
Jörg Richter, Python for Experienced Java Developers
“Union (|): Combines two sets, returning a new set with all unique elements from both sets. Intersection (&): Finds the common elements between two sets, returning a new set. Difference (-): Finds the elements that are only in the first set and not in the second set, returning a new set. Symmetric Difference (^): Finds the elements that are present in only one of the sets, but not in both, returning a new set. Membership (in): Checks if an element is present in the set. Subset (<=), Superset (>=): Checks if a set is a subset / superset of another set.”
Jörg Richter, Python for Experienced Java Developers
“Using dictionary packing in combination with a variable argument list, it is possible to define a highly flexible signature for a function or method: (*args, **kwargs)”
Jörg Richter, Python for Experienced Java Developers
“my_dict = {"c": 3, "a": 1}
func_1(**my_dict)  # 1 0 3 0


# dictionary packing
def func_2(**args):
    print(args)


func_2(x=1, y=2)  # {’x’: 1, ’y’: 2}”
Jörg Richter, Python for Experienced Java Developers
“dictionary packing/unpacking, which transforms a list of function parameters into a dictionary and vice versa.”
Jörg Richter, Python for Experienced Java Developers
“# Comprehension: Create a dictionary
# mapping numbers to their cubes
# and filter even numbers
even_cubes = {x: x*x*x for x in range(1, 10)
              if x % 2 == 0}

print(even_cubes)  # {2: 8, 4: 64, 6: 216, 8: 512}”
Jörg Richter, Python for Experienced Java Developers
“# Check if a key exists in a dictionary
print("name" in my_dict)  # True
print("city" not in my_dict)  # True”
Jörg Richter, Python for Experienced Java Developers
“# Iterating over the dictionary
for key, value in my_dict.items():
    print(key, ":", value)”
Jörg Richter, Python for Experienced Java Developers
“# Removing a key-value pair
my_dict["city"] = "New York"
del my_dict["city"]”
Jörg Richter, Python for Experienced Java Developers
“sorted_list = sorted(string)
sorted_string = "-".join(sorted_list)

print(sorted_list)  # [’A’, ’a’, ’x’, ’z’]
print(sorted_string)  # A-a-x-z”
Jörg Richter, Python for Experienced Java Developers
“print(3 * string)  # xzaAxzaAxzaA”
Jörg Richter, Python for Experienced Java Developers
“Many operations that can be performed on tuples in Python can also be applied to strings.”
Jörg Richter, Python for Experienced Java Developers
“For the same reason, you have to use the tuple constructor if you want to use comprehension for tuples: no_tuple = (i for i in range(5))
print(no_tuple)  # ...

my_tuple = tuple(i for i in range(5))
print(my_tuple)  # (0, 1, 2, 3, 4)”
Jörg Richter, Python for Experienced Java Developers
“creating a tuple that consists of only one element requires an additional comma at the end to avoid ambiguity with a scalar value.”
Jörg Richter, Python for Experienced Java Developers
“Additionally, it is possible to omit parentheses for tuple creation in assignment and return statements: def func(x, y, z):
    return x, y, z


a = 1, 2, 3”
Jörg Richter, Python for Experienced Java Developers
“Finally, it is possible to create reusable slice objects using the slice() function in Python: slice_obj = slice(1, None, 2)  # 1::2

list_1 = [1, 2, 3, 4, 5]
list_2 = [10, 11]

print(list_1[slice_obj])  # [2, 4]
print(list_2[slice_obj])  # [11]”
Jörg Richter, Python for Experienced Java Developers
“shallow_copy = my_list[:]
print(shallow_copy == my_list)  # True
print(shallow_copy is my_list)  # False”
Jörg Richter, Python for Experienced Java Developers
“The :: operator is utilized in Python’s slicing notation to generate a sublist of a list. Its syntax is as follows: list[start:stop:step]”
Jörg Richter, Python for Experienced Java Developers
“even_squares = [x**2 for x in range(1, 10)
                if x % 2 == 0]”
Jörg Richter, Python for Experienced Java Developers
“squares = [x ** 2 for x in range(1, 5)]”
Jörg Richter, Python for Experienced Java Developers
“For generating longer lists with predefined values, Python has a feature called list comprehension.”
Jörg Richter, Python for Experienced Java Developers
“Python list are zero-indexed,”
Jörg Richter, Python for Experienced Java Developers
“Class methods are instance methods on the class object. This means they take a reference to the class object as their first parameter (cls)”
Jörg Richter, Python for Experienced Java Developers
“a static method is a method that belongs to a class rather than any instance of the class. It does not require access to the class or its instances, so it neither takes a reference to the instance (self) nor to the class (cls) as a parameter.”
Jörg Richter, Python for Experienced Java Developers
“@staticmethod and @classmethod,”
Jörg Richter, Python for Experienced Java Developers
“obj.print = types.MethodType(print_x, obj)
obj.print_unbound = print_x”
Jörg Richter, Python for Experienced Java Developers
“To add a function as a method to an instance, create a bound method using types.MethodType:”
Jörg Richter, Python for Experienced Java Developers