Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Write Multiple Statements on a Single Line in Python?

#1
How to Write Multiple Statements on a Single Line in Python?

Problem: Given multiple Python statements. How to write them as a Python One-Liner?

Example: Consider the following example of four statements in a block with uniform indentation:

a = 1
b = 2
c = a + b
print©

Each of the four statements is written in a separate line in a code editor—this is the normal procedure. However, what if you want to one-linerize those:

How to write all four statements in a single line of code?

Solution: The answer is simple if all statements have a uniform indentation and there’s no nested block. In this case, you can use the semicolon as a separator between the statements:

a = 1; b = 2; c = a + b; print©

Let’s do some practice testing to learn and improve your Python skills:

Exercise: one-linerize the given code! Run the code and check if the one-liner does the same as the original code!

Indented Block


While this works beautifully, if all statements are not indented—does it still work if you have an indentation block that starts with the colon : symbol after if, elif, else, for, while, or try/except statements?

Here’s an example of such a block:

for i in range(10): c = i ** 2 print ©

You try the following one-liner using the semicolon as a separator between the two statements in the block

for i in range(10): c = i ** 2; print© '''
0
1
4
9
16
25
36
49
64
81 '''

This works beautifully and Python understands what you are trying to do. However, if you have nested indentation blocks, this doesn’t work anymore.

Consider the following example:

for i in range(3): for j in range(3): print(i, j)

If you write this in a single line, Python throws a syntax error:


While you can discuss if this makes sense or not—given that the syntax is not ambiguous here—it doesn’t change the fact: nested block cannot be one-linerized in a straightforward way. But this doesn’t prevent us from doing it, right?

Nested Indentation Blocks


Read the following article to learn how to compress multiple lines of code into a single line!

Summary: To make a Python one-liner out of any multi-line Python script, replace the new lines with a new line character '\n' and pass the result into the exec(...) function. You can run this script from the outside (command line, shell, terminal) by using the command python -c "exec(...)".



This method is very powerful and it allows you to compress any complicated multi-line script in a single line of Python code!

Python One-Liners Book


Python programmers will improve their computer science skills with these useful one-liners.

Python One-Liners

Python One-Liners will teach you how to read and write “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.

The book’s five chapters cover tips and tricks, regular expressions, machine learning, core data science topics, and useful algorithms. Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You’ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments. You’ll also learn how to:

  Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution
  Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics
  Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning
  Create more advanced regular expressions using grouping and named groups, negative lookaheads, escaped characters, whitespaces, character sets (and negative characters sets), and greedy/nongreedy operators
  Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting

By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line.

Get your Python One-Liners Now!!



https://www.sickgaming.net/blog/2020/08/...in-python/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Python zip(): Get Elements from Multiple Lists xSicKxBot 0 1,973 08-26-2023, 01:28 AM
Last Post: xSicKxBot
  [Tut] Check Python Version from Command Line and in Script xSicKxBot 0 1,911 08-24-2023, 01:34 PM
Last Post: xSicKxBot
  [Tut] Write a Long String on Multiple Lines in Python xSicKxBot 0 1,503 08-17-2023, 11:05 AM
Last Post: xSicKxBot
  [Tut] How to Access Multiple Matches of a Regex Group in Python? xSicKxBot 0 1,489 04-04-2023, 02:26 PM
Last Post: xSicKxBot
  [Tut] How to Correctly Write a Raw Multiline String in Python: Essential Tips xSicKxBot 0 1,492 03-27-2023, 05:54 PM
Last Post: xSicKxBot
  [Tut] How to Split a Multi-line String into Multiple Lines? xSicKxBot 0 1,339 12-12-2022, 09:10 AM
Last Post: xSicKxBot
  [Tut] Python | Split String Multiple Whitespaces xSicKxBot 0 1,334 12-06-2022, 10:04 AM
Last Post: xSicKxBot
  [Tut] Python Print Dictionary Without One Key or Multiple Keys xSicKxBot 0 1,253 10-10-2022, 08:56 PM
Last Post: xSicKxBot
  [Tut] How to Retrieve a Single Element from a Python Generator xSicKxBot 0 1,199 09-30-2022, 11:20 AM
Last Post: xSicKxBot
  [Tut] How to Delete a Line from a File in Python? xSicKxBot 0 1,216 09-24-2022, 10:31 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:

Forum software by © MyBB Theme © iAndrew 2016