How can I write multi-line code in the Terminal use python?
Question:
How can I write multi-line code in the python REPL? :
aircraftdeMacBook-Pro:~ ldl$ python
Python 2.7.10 (default, Jul 30 2016, 19:40:32)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
such as a sample example:
i = 0
while i < 10:
i += 1
print i
In the terminal I don’t know hot to line feed in the python shell:
I tested the Control+Enter, and Shift+Enter, and Command+Enter, they all wrong:
>>> while i < 10:
... print i
File "<stdin>", line 2
print i
^
IndentationError: expected an indented block
Answers:
You can add a trailing backslash. For example, if I want to print a 1:
>>> print 1
1
>>> print
... 1
1
>>>
If you write a , Python will prompt you with … (continuation lines) to enter code in the next line, so to say.
To resolve IndentationError: expected an indented block
, put the next line after while loop in an indented block (press Tab key).
So, the following works:
>>> i=0
>>> while i < 10:
... i+=1
... print i
...
1
2
3
4
5
6
7
8
9
10
Just copy the code and past it in the terminal, and press return. This code works perfect if you do that:
i = 0
..
.. while i < 10:
.. i += 1
.. print(i)
..
1
2
3
4
5
6
7
8
9
10
There comes out:
IndentationError: expected an indented block
So, when use the while loop, the next line should have the indented block(press Tab key).
>>> i = 0
>>> while i < 10:
... i += 1
... print i
...
1
2
3
4
5
6
7
8
9
10
>>>
Utilize the python3 - <<'EOF'
command.
For instance:
python3 - <<'EOF'
a=7
b=5
print(a+b)
EOF
12
Python automatically detects code blocks in sections like for-next, while, etc. Just put a ‘:’ <– Colon symbol after some code.
Then the next line will have a continuation symbol (‘…’) in front of it instead of the prompt (‘>>>’)
Remember to press a tab to indent the code that you want to execute in the block. That will indent the line and tell Python that the code that follows is a part of the block.
Different approaches for the same problem.
This solution might be complex, but working my ubuntu
one line of python script can
- import yaml and sys lib
- lambda-function read yaml file by using first sys.argv1
- get expected value from return nested-dictionary from lambda function by using sys.argv2 sys.argv3
- print yaml_as_dict will load test_var as variable
- check test_var by
echo $test_var
`
$
$ test_var=$(python3 -c $"import yaml,sys; yaml_as_dict=(lambda :yaml.safe_load(open(f'{sys.argv[1]}','r').read()))()[sys.argv[2]][sys.argv[3]]; print(yaml_as_dict)" <argv1> <argv2> <argv3>)
$
$ echo $test_var
$
How can I write multi-line code in the python REPL? :
aircraftdeMacBook-Pro:~ ldl$ python
Python 2.7.10 (default, Jul 30 2016, 19:40:32)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
such as a sample example:
i = 0
while i < 10:
i += 1
print i
In the terminal I don’t know hot to line feed in the python shell:
I tested the Control+Enter, and Shift+Enter, and Command+Enter, they all wrong:
>>> while i < 10:
... print i
File "<stdin>", line 2
print i
^
IndentationError: expected an indented block
You can add a trailing backslash. For example, if I want to print a 1:
>>> print 1
1
>>> print
... 1
1
>>>
If you write a , Python will prompt you with … (continuation lines) to enter code in the next line, so to say.
To resolve IndentationError: expected an indented block
, put the next line after while loop in an indented block (press Tab key).
So, the following works:
>>> i=0
>>> while i < 10:
... i+=1
... print i
...
1
2
3
4
5
6
7
8
9
10
Just copy the code and past it in the terminal, and press return. This code works perfect if you do that:
i = 0
..
.. while i < 10:
.. i += 1
.. print(i)
..
1
2
3
4
5
6
7
8
9
10
There comes out:
IndentationError: expected an indented block
So, when use the while loop, the next line should have the indented block(press Tab key).
>>> i = 0
>>> while i < 10:
... i += 1
... print i
...
1
2
3
4
5
6
7
8
9
10
>>>
Utilize the python3 - <<'EOF'
command.
For instance:
python3 - <<'EOF'
a=7
b=5
print(a+b)
EOF
12
Python automatically detects code blocks in sections like for-next, while, etc. Just put a ‘:’ <– Colon symbol after some code.
Then the next line will have a continuation symbol (‘…’) in front of it instead of the prompt (‘>>>’)
Remember to press a tab to indent the code that you want to execute in the block. That will indent the line and tell Python that the code that follows is a part of the block.
Different approaches for the same problem.
This solution might be complex, but working my ubuntu
one line of python script can
- import yaml and sys lib
- lambda-function read yaml file by using first sys.argv1
- get expected value from return nested-dictionary from lambda function by using sys.argv2 sys.argv3
- print yaml_as_dict will load test_var as variable
- check test_var by
echo $test_var
`
$
$ test_var=$(python3 -c $"import yaml,sys; yaml_as_dict=(lambda :yaml.safe_load(open(f'{sys.argv[1]}','r').read()))()[sys.argv[2]][sys.argv[3]]; print(yaml_as_dict)" <argv1> <argv2> <argv3>)
$
$ echo $test_var
$