This question already has answers here:
How do I execute a string containing Python code in Python?
(14 answers)
Closed 2 years ago.
I have a little problem, I have written a for loop as a string.
In PHP, with the help of function exec(), we can run the string which will eventually run the for loop defined inside the string.
Can we do such a thing in Python as well?
By example, I would like run follow it:
string="for i in range(1,(5+1)): print(str(i))"
How to run this in Python?
You can use exec if you want to execute some statements:
code = 'for i in range(1,(5+1)): print(str(i))'
exec(code)
If you want to evaluate an expression and get the value then you can use eval:
value = eval('2+3')
print(value) # 5
Related
This question already has answers here:
What do backticks mean to the Python interpreter? Example: `num`
(3 answers)
Meaning of the backtick character in Python
(2 answers)
Closed 1 year ago.
Lots of old python code I look in has this ` symbol around a lot of stuff, what does it do? Now it is not considered valid syntax, obviously.
And I don't think it is just another string identifier, its sometimes wrapped around functions in the code I'm looking at.
Any help will be appreciated.
This question already has answers here:
In Python if you return a string,it will be displayed with quotes around it but If you print the string, it will not be shown with quotes. why? [duplicate]
(2 answers)
In Python what is it called when you see the output of a variable without printing it?
(3 answers)
Closed 2 years ago.
What is the difference between 'python' & python?
We get outputs in different forms for accessing the elements of a list.
Print statement returns output without single quote.
This is the behavior of the python REPL - when using python interactively, the returned value of whatever statement you run (as long as it is not None) gets printed to the console.
Since list1[0] returns the string 'python' and there's no more statement after that, the string gets printed. If you're running the same line in a script (not interactively), then it wouldn't print anything.
print() specifically prints to the console, and will do so regardless of whether python is running in interactive mode or not. It also formats the output to be printed - it doesn't print "what the element is", it prints "what you tell it to print". Which is why the single-quotes aren't there. print() doesn't need to say that it's a string.
This question already has answers here:
How do I execute a string containing Python code in Python?
(14 answers)
Closed 2 years ago.
I have a string 'user = input("Hello World")'. How can I execute the string to be able to run as a python command?
I am a new to python so I must have missed the basic syntax. please help me instead of routing me to another answer
You can use either of two exec or eval to run the string as python command based on your desired syntax or result
eval('user = input("Hello World")')
exec('user = input("Hello World")')
Visit exec and eval to learn more about them
This question already has answers here:
What is the purpose of the return statement? How is it different from printing?
(15 answers)
Closed 6 months ago.
import urllib,re
def getver():
url='https://github.com/Bendr0id/xmrigCC/releases'
website = urllib.urlopen(url)
html = website.read()
links = re.findall(r'(?<=<a href=")[^"]*\bgcc-win64.zip\b', html)
link=links[0]
version=link.split('/')
ver0=version[5]
return ver0
getver()
I've tried to run the code but it doesnt output anything,instead when I replace return with print it prints out the correct answer which is 1.5.2 .
What am I doing wrong?
You have been fooled by the interactive interpeter's friendly habit of printing out the results of any bare expressions you enter.
This does not happen when you run a program, so you need to ensure you output values specifically by using the print statement.
This is specifically mentioned in a rather obscure portion of the Python documentation dealing with the language's grammar.
Change the last line to:
print(getver())
This question already has answers here:
Python Named Argument is Keyword?
(2 answers)
Closed 6 years ago.
I'm writing a python script wherein I need to use the continue word.
I know that it is a python keyword.
So how should I write my script so that python will not complain that I am using a keyword as a string literal.
Thanks in advance for your time.
You should use something like:
_continue
But why not using more descriptive and longer variable name?! like:
continue_whatever or go_on ...
The normal practice is to use a trailing underscore to use a keyword that already being used by the language, like continue_ or input_.
I don't think anyone would think that just overwriting keywords is every a good idea.