Sympy - print(limit()) as latex - python-3.x

Probably this is a question easy to answer but I couldn't find any solution that worked for me. I want to have the content of print() to be displayed as latex.
Edit: I'm using jupyter.
import sympy
from sympy.assumptions import assuming, Q
sympy.init_printing()
K,L,alpha = sympy.symbols("K,L,alpha")
Y = (K**alpha)*(L**(1-alpha))
Y
with assuming(Q.is_true(L == 1)):
print(sympy.limit(Y,K,1,"-"))
sympy.limit(1/alpha,L,0)
For the calculation without assumption (last code line) everything works fine.
I have already tried latex(print()) but that only gave me latex code but no latex output.
Best,
Fabian

You could do the following. Insert at the beginning of the code
from IPython.display import display
and replace print(...) with display(...), e.g.,
display(sympy.limit(Y,K,1,"-"))

Finally I used the following solution:
sympy.limit(Y.subs(L,1),K,1,"-")
Easy and works fine ...

Related

In python,I want to use \b to backspace but it shows 0x08,how to fix this problem

print("a\bc", end="", flush=True)
#I hope the out put is c rather than a0x08c
the result
I tried out your code it works perfectly fine for me, it printed https://i.stack.imgur.com/bA97f.png
I'm guessing something wrong with the editor or python itself, but not really sure.
you can once try the code in IDLE, Jupyter Notebook / Lab, or Terminal.

How to transform lines of code into a comment in Python3

just wondering how to convert a set of code into comments, without having to go through each individual line of code and put a # at the beginning. Was thinking that maybe there was a quick way to highlight the code and then convert it into comment, atm i'm just cutting it off the code and then re-pasting it in later on.
Much appreciated :)
P.S. i m using Pycharm as my IDE
In jupyter notebook, we select lines of code and press ctrl and / key simultaneously to convert a set of code into comments. Also same for vice versa.
You can try it in Pycharm.
You can use the (""") symbol before and after the text you want to comment out. It is not exactly a comment and more of a text constant, but either way it is often used for documentation, so you can use it for multiline comments except for some cases when it causes unexpected errors
To avoid such cases you could simply use multiple single line comments by selecting the desired lines and using ctrl+/ to comment them out

hidding the runfile() command in Spyder

Is there a way to 'hide' the runfile() command so that it doesn't get displayed on the IPython console? It can get really annoying when the file has a long path since it displays the path twice:
runfile('C:/Users/One/Desktop/Training/Week1/Files/file1.py',wdir='C:/Users/One/Desktop/Training/Week1/Files/file1.py')
I agree with you, I hate having a cluttered IPython console .
I found a way while I was trying to find a solution to clear the old text in the console when starting a script:
Define an anonymous function (reference: https://python-forum.io/Thread-Difference-between-os-system-clear-and-os-system-cls) at the beginning of your script to clear the console from text
cls = lambda: print("\033[2J\033[;H", end='')
cls()
The annoying runfile (...) text section will disappear :)
(Spyder maintainer here) This is not possible in our current stable version (Spyder 4), sorry.
This is how I made Alexis' idea work reliably for me. My screen got updated by my code before cls() finished executing, consequently it was blanked over.
from time import sleep
def cls():
print("\033[2J\033[;H", end='')
sleep(0.1)
cls()

Python 3 - recognizing minus signs in a text string

I'm using Python 3.5 and import a text file as follows
with open(fn) as f:
data = f.read()
I then notice that there's a space between the minus sign and the digits of a negative number (e.g. \n\t- 2.51\t). I have tried to close the gap by writing
data.replace('- ','-'), but nothing happens. Oddly enough, this works like a charm in a Python console, but not in code. How can I solve this problem?
Is this a Unicode issue? Is it possible that the - I type on my keyboard is different from the - in the file? If so, how can I tell the two -'s apart?
Thanks in advance for your assistance
Thomas Philips
I made an elementary error, and wrote
data.replace('- ','-'),
when I should have written
data = data.replace('- ','-').
As soon as I did this, the problem solved itself.

Python colorama not working with input?

Finally got colorama working today, and it works excellent when printing strings, but I got the common error everyone seems to get when I attempted to use colorama with input.
Here's my code:
launch = input(Fore.GREEN + "Launch attack?(Y/N): ")
Screenshot of output:
I had this same issue (Python 3.5.4) and, just in case it is not too obvious for somebody else looking at this, you can always rely on the workaround of combining print / input calls where you previously had just an input call:
print(Fore.GREEN + "Launch attack?(Y/N): ", end='')
launch = input()
This should produce the exact same output as in your question, with no extra blank lines and with the code coloring working without the need of importing anything else.
The (small?) disadvantage is that you you will end up with two lines of code where you previously had just one.
On my system, input() works with colors if you add
import sphinx.quickstart
to your module.
So here is the full code.
from colorama import Fore
import colorama
import sphinx.quickstart
colorama.init()
launch = input(Fore.GREEN + "Launch attack? (Y/N): ")
(This leads to two questions:
Why does it not work in the first place?
What is the actual reason? – Someone might like to dive into the sphinx source code.)
N.B. if you run python via winpty from Git Bash, set convert.
colorama.init(convert=True)
Otherwise, you do not get color with the current versions.
To get rid of this problem in the starting of the code add
import os
os.system('cls')
This will clear the screen and hence clear all the external factors blocking the colorama in input.
This works 100% you just need to do it once in the starting of the program [or just before the first use of colorama with input] and after that use colorama in any creative way you want to.
I hope this will help all the creative minds trying to make their codes more colourful
just make sure about the 'autoreset' in init()
init(autoreset=True)

Resources