Daily Python Puzzle: String Concatenation

What is the output of this code snippet?   print(3 * 'un' + 'ium')   Python has powerful built-in capabilities for string manipulation. Web companies like Google love Python because it is a perfect fit for the text-based World Wide Web. The puzzle introduces two basic string manipulation operators. The ‘+’ operator concatenates two strings. The ‘*’ operator concatenates a …

Daily Python Puzzle: Integer Division

What is the output of this code snippet?   x = 50 // 11 print(x)   When I started to learn Python 3, I used to be confused about the semantics of dividing two integers. Is the result a float or an integer value? The reason for my confusion was a nasty Java bug that I once found in my …

Daily Python Puzzle: Slicing

What is the output of this code snippet?   word = "galaxy" print(len(word[1:]))   More than one out of four Finxter users can not solve this puzzle. There are two concepts that are novel for them: the len() function and slicing. The len() function is a handy tool to get the length of built-in Python data types such as strings, …

Daily Python Puzzle: List Indexing

What is the output of this code snippet?   squares = [1, 4, 9, 16, 25] print(squares[0])   This puzzle introduces the simple but powerful list data structure in Python. You have to search very hard to find an algorithm that is not building upon a list. Many famous algorithms such as quicksort are based on only one list as …

Daily Python Puzzle: String Indexing

What is the output of this code snippet?   x = 'silent' print(x[2] + x[1] + x[0] + x[5] + x[3] + x[4])   This puzzle introduces a powerful tool for your Python toolbox. Make sure, you feel comfortable using it because many advanced puzzles depend on it. The name of the tool is indexing. In Python, you can access …

Daily Python Puzzle: Comments

What is the output of this code snippet?   # this is the first comment spam = 1 # and this is the second comment # … and now a third! text = "# Is this a comment?" print(text) This puzzle introduces two basic concepts. First, variables can hold strings. In fact, variables can hold any data type. The interpreter …

Daily Python Puzzle: Basic Math Operations

What is the output of this code snippet?   x = 50*2 + (60-20)/4 print(x) The Python interpreter is a powerful tool. In this puzzle, it acts as a simple calculator. It takes a basic mathematical expression and calculates the result. The syntax of expressions is straightforward: use the operators +, -, * and / exactly as you have learned …

View Post

DAILY PYTHON PUZZLE: Python Division

What is the output of this code snippet?   x = 55 / 11 print(x) The majority of people solve this puzzle correctly. Even so, the first goal of this puzzle is to introduce the concept of variables. Python evaluates the result of the expression on the right side of the equation and stores it in the variable x. After …

Daily Python Puzzle: Basics of Boolean Logic

What is the output of this code snippet?   # Influential reddit users # in million karmas way_fairer = 2.7 StickleyMan = 2.3 _vargas_ = 2.3 smeeee = 1.3 a = way_fairer > StickleyMan b = StickleyMan < smeeee and a c = _vargas_ >= StickleyMan c = a and b or c and smeeee > 1.2 d = not …

Daily Python Puzzle: String Formatting

What is the output of this code snippet?   # The number of units per player units = {} units['Alice'] = 10 units['Bob'] = 15 x = 'Alice' y = 'Bob' d = units['Bob'] – units['Alice'] print('{} has {} units less than {}'.format(x,d,y)) The string format function is a useful feature that is new in Python 3. Use it to …

View Post

Daily Python Puzzle: Set, Lambda, and Filter

What is the output of this code snippet?   # popular instagram accounts # (millions followers) inst = {"@instagram": 232, "@selenagomez": 133, "@victoriassecret": 59, "@cristiano": 120, "@beyonce": 111, "@nike": 76} # popular twitter accounts # (millions followers) twit = {"@cristiano": 69, "@barackobama": 100, "@ladygaga": 77, "@selenagomez": 56, "@realdonaldtrump": 48} inst_names = set(filter(lambda x: inst[x]&gt;60, inst.keys())) twit_names = set(filter(lambda x: twit[x]&gt;60, …

Daily Python Puzzle: Parameter Passing

What is the output of this code snippet?   def depreciation1(asset_value, rate): asset_value = asset_value * rate def depreciation2(asset_values, rate): for i in range(len(asset_values)): asset_values[i] = asset_values[i] * rate asset_value = 10 depreciation1(asset_value, 0.9) print(asset_value) my_asset_values = [10, 100, 1000] depreciation2(my_asset_values, 0.9) print(int(my_asset_values[0])) This puzzle demonstrates that parameters are called by object in Python. What does that mean? We look …