“
words = [ 'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes', 'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the', 'eyes', "don't", 'look', 'around', 'the', 'eyes', 'look', 'into', 'my', 'eyes', "you're", 'under' ] from collections import Counter word_counts = Counter(words) top_three = word_counts.most_common(3) print(top_three) # Outputs [('eyes', 8), ('the', 5), ('look', 4)]
”
”
David Beazley (Python Cookbook: Recipes for Mastering Python 3)
“
What will be the output after the following statements? m = 6 while m < 11: print(m, end='') m = m + 1 a. 6789 b. 5678910 c. 678910 d. 56789
”
”
S.C. Lewis (Python3 101 MCQ - Multiple Choice Questions Answers for Jobs, Tests and Quizzes: Python3 Programming QA (Python 3 Beginners Guide Book 1))
“
prints L1 = [[2, 3]] L2 = [[2, 3]] because both L1 and L2 contain the object that was bound to L in the first assignment statement.
”
”
John V. Guttag (Introduction to Computation and Programming Using Python: With Application to Computational Modeling and Understanding Data)
“
Code: import pandas as pd print('-------PRICE LIST OF THREE PRODUCTS BASED OF SIZE-------') price=pd.DataFrame( {"Large":[75,200,55],"Medium":[50,120,30] }, index=['Ice Cream','Pizza','Coke'] )
”
”
Ryshith Doyle (PYTHON FOR DATA ANALYSIS: Master the Basics of Data Analysis in Python Using Numpy & Pandas: Answers all your Questions Step-by-Step (Programming for Beginners: A Friendly Q & A Guide Book 2))
“
repeater = BoundedRepeater('Hello', 3)
iterator = iter(repeater)
while True:
try:
item = next(iterator)
except StopIteration:
break
print(item)
”
”
Dan Bader (Python Tricks: A Buffet of Awesome Python Features)
“
print "Hello World!" Write this code in a text file and save it as main.py. Execute the code by writing main.py on the python command prompt to get the desired result.
”
”
Cooper Alvin (Computer Programming For Beginners: Learn The Basics of Java, SQL, C, C++, C#, Python, HTML, CSS and Javascript)
“
a) =Assigns values from right side operands to left side operand >>>a=2 Variable a takes the value 2 b) +=Adds values on either side of the operator and assign the result to left operand >>>a=2 >>>a+=1 >>>print(a) 3
”
”
Ryshith Doyle (Python Programming For Beginners And Python For Data Analysis: Master the Basics of Data Analysis in Python Using Numpy & Pandas Answers all your Questions ... Beginners: A Friendly Q & A Guide Book 4))
“
>>>print(b) a is less than 10
”
”
Ryshith Doyle (Python Programming For Beginners And Python For Data Analysis: Master the Basics of Data Analysis in Python Using Numpy & Pandas Answers all your Questions ... Beginners: A Friendly Q & A Guide Book 4))
“
Next, it opens the dictionary and iterates through each word in the dictionary, creating an encrypted password hash from the dictionary word and the salt. If the result matches our encrypted password hash, the function prints a message indicating the found password and returns. Otherwise, it continues to test every word in the dictionary.
”
”
T.J. O'Connor (Violent Python: A Cookbook for Hackers, Forensic Analysts, Penetration Testers and Security Engineers)
“
for x in range( 1,255): ... print “192.168.95.” + str( x)
”
”
T.J. O'Connor (Violent Python: A Cookbook for Hackers, Forensic Analysts, Penetration Testers and Security Engineers)
“
Technically, __str__ is preferred by print and str, and __repr__ is used as a fallback for these roles and in all other contexts.
”
”
Mark Lutz (Learning Python: Powerful Object-Oriented Programming)
“
Because printing runs __str__ and the interactive prompt echoes results with __repr__, this can provide both target audiences with an appropriate display.
”
”
Mark Lutz (Learning Python: Powerful Object-Oriented Programming)
“
class Indenter:
def __init__(self):
self.level = 0
def __enter__(self):
self.level += 1
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.level -= 1
def print(self, text):
print(' ' * self.level + text)
”
”
Dan Bader (Python Tricks: A Buffet of Awesome Python Features)
“
With the MAC address of a wireless access point, we can now also print out the physical location of the access point as well. Quite a few databases, both open-source and proprietary, contain enormous listings of wireless access points correlated to their physical locations.
”
”
T.J. O'Connor (Violent Python: A Cookbook for Hackers, Forensic Analysts, Penetration Testers and Security Engineers)
“
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)
“
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)
“
print(3 * string) # xzaAxzaAxzaA
”
”
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)
“
# Iterating over the dictionary
for key, value in my_dict.items():
print(key, ":", value)
”
”
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)
“
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)
“
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)
“
def create_multiplier(factor):
def multiplier(x):
return x * factor
return multiplier
triple = create_multiplier(3)
print(triple(5)) # 15
”
”
Jörg Richter (Python for Experienced Java Developers)
“
squared = map(lambda x: x ** 2, numbers)
print(list(squared)) # [1, 4, 9, 16, 25]
evens = filter(lambda x: x % 2 == 0, numbers)
print(list(evens)) # [2, 4]
”
”
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)
“
print(i, end=" ") # 3 5 7 9
The end parameter in the print() function specifies the character or string to print at the end of the output, replacing the default newline (\n).
”
”
Jörg Richter (Python for Experienced Java Developers)
“
def func(a=[]):
a.append(1)
return a
print(func()) # Output: [1]
print(func()) # Output: [1, 1]
”
”
Jörg Richter (Python for Experienced Java Developers)
“
In Python, the / symbol is used in function definitions to indicate that parameters preceding it are positional-only. This means they can only be specified by position and cannot be passed as keyword arguments. Parameters following the / are either positional-only or positional-or-keyword. def func(a, b, c, /, d, e):
print(a, b, c, d, e)
func(1, 2, 3, 4, 5) # 1 2 3 4 5
func(6, 7, 8, 9, e=10) # 6 7 8 9 10
# This does not work
# func(6, 7, c=8, d=9, e=10)
”
”
Jörg Richter (Python for Experienced Java Developers)
“
In the example above, the statement obj1.x = "Good Morning" is possible but would create an instance variable obj1.x, so obj1.print_x() would still print the old value of x and not "Good Morning".
”
”
Jörg Richter (Python for Experienced Java Developers)