Wolfram Language for Python Users

Dimitar compares Python and Wolfram Language, showing how to use them together. Python is open-source and library-centered; Wolfram Language has 6,300 built-in functions and runs in Mathematica 14.1. You can call Python from Mathematica with ExternalEvaluate or call Wolfram from Python using wolframclient in Jupyter.
Questions this video answers
Yes — to use WolframLanguageSession locally you must have a Wolfram installation and a valid license on the machine (alternatively use the Wolfram Cloud evaluator with API keys/authentication).
About 6,300 (Mathematica 14.1 referenced).
Sam.
Good afternoon everyone. Sorry for the delay. I hope you are all well. Welcome to today's webinar which is entitled Wolfram Language for Python Users. My name is Chris and I work in the IT department here at Wolfram. The topic today will be presented by demtar, who is a technical content writer. If you have any questions, please feel free to log them using the Q and A tab on your screen and dematar will answer them as the talk goes on or at the end. The Notebook URL can be accessed in the pinned message at the top of the chat. If you experience any technical issues during the webinar, please refresh or restart your browser and rejoin the webinar. This should fix the issue. Webinar now begin. I hope you all enjoy. Over to you, Dimitar.
Thank you Chris. And welcome everyone to this webinar about Wolfram language for Python users. It's a webinar that I'm quite excited to give actually because I have been using Python for quite some time now. I'm familiar with the language, it feels comfortable, it feels very useful. Obviously since joining Wolfram, I've been using Wolfram language a lot, extensively. I enjoy that as well. So I think combining both languages opens up new possibilities for workflows and other things which I will be showing you in this webinar. Before I start, before I start, I would just like to tell you that you're probably, for those of us watching us on BigMarker, you're probably going to be seeing a few poll questions just asking you whether you're a user of Full from Tech and some some other questions that help us when we're thinking about creating new webinars. We just want to make sure that when we create something new, it's useful for everyone. So if you take a second to answer those questions, if and when they pop up, we appreciate that. And so I'll just give a few seconds for that and then I'll start with the webinar. Okay, let's get into it. So this is what you're seeing on the screen is just a brief overview of what we are going to be covering today. So we'll start with an introduction, a brief history of each language, broad comparison of their similarities, differences, what the code looks like in each language, out of the box functionality which is going to be obviously emphasized more. On the Wolfram language side, we'll get to some actual examples. I'll probably not go into too much detail into some of those just because we have more interesting examples down here. When we get to actually using the other language from the environment of one language, but I'll explain just broadly what they're about. So we'll start using Python from Wolfram Mathematica. So from this environment that we are in now, and then I will talk about using Wolfram language in Python environment. So in the notebook that you have, that you have access to, at least on BigMarker, I believe there should be a link on YouTube as well. But in the notebook it's a zip file that has quite a few. I'm not sure if this is very big. I think you can see. So the, the notebook that we're starting with is this Wolfram language for Python users. But then later in the webinar I will be going through some of these Jupyter notebooks, depending on how much time I have, because these materials here are probably. I wouldn't be able to cover that in an hour and a half. But I'll try to get to the main points of the Jupyter notebooks, just because I'm aware that we probably have quite a few Python users who are interested not only in how to use Python from Wolfram Mathematica, but also how to use Wolfram language from something like Jupyter Notebook. We'll be talking about Jupyter notebooks, but of course you can just use the Wolfram language evaluator, just from a plain Python evaluator environment, could be a text file, could be anything. And then finally I'll have some tips for Python users, which is in this small document that I have here. I didn't really have a chance to make an extensive list of tips, but there are just some little things that I, as someone who came into W from, as someone who had experience in Python, it's some things that I wish I knew at the beginning. So hopefully you'll find those useful as well. Okay, so let's get into it. Some of the key questions that we're going to answer in this webinar is how to swoop from language stack up to Python. And actually, I disagree with this framing slightly. Maybe not here, but a few times in this notebook, the two languages are presented as kind of as if there's a competition between them. I don't see it that way. I mean, for fundamentally we need to remember that programming languages are just tools and they have value insofar as they help you to accomplish something that you want to accomplish and not anything beyond that. So I see them as both, both of both Wolfram language and Python are just very Good tools, very useful tools. There's no, at least in my mind, there's no need to see it as a competition. It's just combining both of them is actually quite good for both, I would say. But we are definitely going to cover functionality, usability, interface. Then what is transferable between Python and Wolfram language? It's quite important. Obviously we'll talk about how if you're using, let's say, Wolfram language from a jupyter notebook and you evaluate an expression that returns an integer, that's fine and you can use that directly in Python. If it's a complicated object like I don't know, 3D plot or even images, then it becomes a bit more complicated. But obviously there are workarounds that I'll talk about as well. And yes, the best practices for a combined experience. And this will lead nicely into my tips for Python users. Okay, so to start with a brief history, Python has been around since 1991. Currently it's on version 3.12. I think 3.13 is now in development or alpha version or whatever they call it. So it will be out soon. And Python is an object or it really is a multi paradigm language. You can use it as an object oriented language and it also depends on which libraries you use because some of them are definitely object oriented, otherwise you can just use it as a functional procedural language. But Python emphasizes readability and it's quite a high level language by which we mean that it's more user friendly than some of the low level languages. And readability, yes, it really emphasizes when you look at a piece of Python code, usually depends obviously on the code, but usually you are able to make sense of it, hopefully. So the first Python Notebooks appeared in IPython, inspired by mathematical notebooks. So this whole notebook paradigm that we talk about is essentially what you see here in Mathematica. This is a notebook. And what a notebook essentially is is I'm sure many of you are familiar with this, but just for the benefit of those who are not, notebooks are just a type of document that combines traditional programming code inputs and outputs with other elements such as text, images and so on. So what you see here, I have quite a nice formatted text with titles, subtitles, bullet points, hyperlinks, and then a bit later on I have programming code that I can evaluate in the same environment. Usually notebooks are kind of contrasted with either with more traditional programming environment such as command line interfaces or just plain text files. I guess obviously even if you're writing a text file let's say you're writing in Python script and whatever in semi text editor of your choice. You can have comments and comment blocks are something that the interpreter ignores. So they're essentially just text. But obviously you don't have anything like nice formatting or a nice interactive documents. So what a notebook actually is. So the value of a notebook is that you can have this kind of a live document that you can edit any part of the history of it, you can combine different types of input and so on. So notebooks are quite nice. Wolfram was the first to implement notebooks then obviously Python implemented them in 2001. I think IPython is the predecessor of Jupyter, which is now probably the most common notebook environment. Jupyter's I think they say that they're language agnostic. I think they're mostly used for Python though. But they do support other languages like Julia. I think R is also supported a few other languages. But the basic idea is the same. You still have these documents which are combined different types of input. Mathematica, so first released in 1988, so a bit older than Python. Currently it's on version 14.1, which was released I believe this month or the last day of last month. Usually when we talk about Mathematica we talk about a combined kernel, which is what does the actual evaluation of the expressions you write and a front end notebook interface, which is Mathematica, which is this software. So you have everything integrated into one piece of software software, which is Mathematica. One of the main kind of distinctive features of Wolfram language is symbolic computation. Again, probably you're probably familiar with this, but the idea is that everything in Wolfram language is a symbolic expression which allows you to do some really cool things, which actually we're going to get into some of them a bit later on in the webinar. So I'm not going to say too much about it here. Emphasizes computation, automation allows for arbitrary structures and data. These are just some of the characteristics of the Wolfram language. Again, both languages are really popular, really useful. Each of them have their strengths. One thing I would mention right away about the Wolfram language is that its documentation is excellent. I know it probably sounds like a basic thing, but if you use different programming languages you probably know that that's not always the case. And having excellent accessible documentation really, really helps when you're writing code. Just as a side note, what is the difference between Mathematic and Wolfram language? I think Wolfram probably hasn't done the best job that it can do in kind of differentiating those two. But the main difference is that Wolfram Language is the actual programming language. You can write this language in Mathematica, which is the front end software, but you can write it anywhere else you want. Some other IDEs like VS Code, IntelliJ Eclipse fully support Wolfram language. So you can write Wolfram Language in those if you prefer. But I think typically Wolfram Language users prefer Mathematica. Obviously it's designed for the Wolfram language, so everything is really nicely integrated and, and it just works well. But that's the kind of the distinction between Mathematica and Wolfram language. In addition to IDEs, you can also write just plain Wolfram language code. You can have packlets, you can have command line scripts, or you can write Wolfram Language on the cloud. So we have this Wolfram Cloud product which you write Wolfram Language and then the actual evaluation happens on Wolfram servers. It doesn't use your PC. There are some trade offs with that, but that's another topic. So, So that's the brief history of, of each launch. So let's look at the actual comparison. Now. There are obviously a lot of similarities and there are a lot of differences as well. So when we talk about combining both languages, it's a good, it's a good place to start to talk about what these are. So in terms of similarities, both Python and Wolfram Language are low preamble interactive languages. So what this means usually is another way to say this is user friendly. Low preamble means that there's a low barrier of entry. I mean there's two programming languages so there's some learning to be done if you want to use them. But definitely compared to some other languages, you can start using Python right away, you can start using Wolfram Language right away. And I mean that's, that's part of the reason why they, they have both become so popular. Another similarity is that they both, both focus on computation, supporting science and mathematical concepts. Even though I would say both of them are perfectly fine for more general applications as well. But it is true that both Wolfram Language and Python have extensive support built in functionality and external resources that support science. I would say in the case of Python, probably more centered around data science with things like Pandas and Numpy and things and nice libraries like that. More recently, probably machine learning, neural nets, LLMs, things like that, Pytorch, a lot of things are done in Python as well. Both languages support a number of different ides. I just touched on them When I was talking about Wolfram language and Mathematica. But you can, you can write Python or Wolfram language code in VS code in IntelliJ, in Eclipse, shell scripts, notebooks and so on. You can write, and this is the point of the webinar, you can write Python code in Mathematica and you can write Wolfram language code in Python. So that's an additional element here. Both of them are considered multi paradigm. Okay, so here it says multi paradigm, not object oriented, which I probably agree with more. As I mentioned, you can write Python code as an object oriented, you can write classes and methods on these classes and so on, but you can also use them. And I think it's probably more common to use them as functional procedural languages, but they support whatever, whatever suits you essentially. And they're both high level languages. Again, this is a bit similar to low preamble interactive languages, but high level languages just means closer to the user rather than closer to the cpu. It's not that important. Again, I would interpret this as user friendly. I mean it is important, but just not for this webinar. I guess both of them allow for code delivery in multiple forms, especially move from language things like creating APIs. Just publishing your code online, publishing the results of your code online is super easy. It's not really that difficult in Python either. So both of them are really good for that. And both of them do not require memory management or compilation for most application. Practically this means that both of them are interpreted languages. Although for Wolfram language we have some really cool people here working on a compiler for the Wolfram language. I'm not sure, well, I'm not sure what the vision is for the compiler, how big it's going to become, how big of a part in the overall language is going to become. But it's really interesting to see. But again, for the context of this webinar, don't, we don't need to, don't need to think about that. You can think of both of them as interpreted languages. So those are the similarities between the two languages. There are some differences as well, of course. Probably the main one is that Python is an open source language, meaning that not only can you access it for free, but you are able to inspect the source code, you can modify it if you wish, you can do anything you want with it. Yeah, Python is free to use, but a number of implementations and dialects. I'm not completely sure what, what they mean by dialects here. I guess the only thing I can think of is the transition between Python 2 and Python 3, which broke some backwards compatibility. So the two of them are slightly different now, but other than that I can't, I can't really think of different dialects for Python. If anyone has an idea, can leave a comment, I'll be interested in that. Oh yeah, so here it mentioned some functionality is not forwards backwards compatible with major releases. So that was. Yeah, that was infamously the case when Python Transitioned from Python 2 to Python 3. It broke a lot of people's code and it made a lot of people unhappy. But it happens. Python ships with minimal primitives and relies on externally developed packages. That is certainly true. There are a minimal number of built in functions in Python. Even if you count the kind of the base modules like OS and things like that, it's still a very minimal set of base functionality. And typically if you're writing a Python code and you want to do anything that's remotely complicated, you will need to write your own code. So the Wolfram language is a single source language developed in house at Wolfram Research. It is proprietary, but instant centralized maintenance of language and documentation. Here again, when we talk about open source and proprietary, there are essentially trade offs that come with each of those philosophies, if you will, or business models more practically. Obviously with open source you get a free language first of all, but you get a. It's easier to spread in the community, people start writing things, but then the trade off is that linking different things written by different people usually becomes difficult. There could be a lack of central vision which is back to the whole Python 2 to Python 3 transition and so on, so on. The Wolfram language side is a proprietary license, which again, the trade off there is that you have to pay money to use it essentially. Although with Wolfram Engine you can technically use it for free, but if you want the nice IDE and the nice cloud features and the nice LLM features, essentially you have to pay. But obviously the benefit of that is that because it's proprietary, because of that business model allows Wolfram to kind of have a central control and hire people to really polish the language. I would say, and I mentioned that Wolfram language has excellent documentation. That's part of the reason, because there are people whose job is to write excellent documentation. So it's not one paradigm or one philosophy or one business model is not necessarily better than the other. It's just different trade offs. And that's one of the cool things actually in being able to combine both languages, is that you can take the best of both. Essentially Wolfram language, all internal Functions are backwards compatible, virtually all internal functions. This is true. You can heard a story from a colleague of mine that he had a notebook from 2002 I think, and he was able to run it with the latest version of Mathematica, which is really cool. That wouldn't happen in Python. I wouldn't think so. A great deal of functionality provided out of the box. Yeah. So one of the distinctive features of Wolfram language, especially when compared to Python, is that it has a lot more built in functions. So here it says how many? 6,000. A bit over 6,000. I wonder if that's not more with 14.1 now I think we're getting closer to 7,000. I wonder how they. Let me just have a quick look at the source for this graph. So we have the Python functions or the mathematical ones. Length, entity list, Wolfram language symbol. Give me a quick. I want to do a quick check. Okay, so it's 6,000. Okay. I thought we were getting closer to 7,000, but apparently not. Oh, Minecraft disappeared. Okay, there it is. Okay, so yeah, the, the general point still stands that in terms of just basic functionalities that come with the language that you don't have to go to external packages for or you don't have to write your own things for, many of especially quite simple things that you would have to write in languages like Python come out of the box and move from mathematically, which is quite nice. Do I have anything more to say here? No, I think that's it. For the similarities and differences between the two languages, we can look at the code structure. They're slightly similar. Obviously there are differences in syntax. So brackets and commas in Wolfram language are indentations in Python. Obviously Python has a whole meaningful indentation thing, which I personally find pretty cool. But Wolfram language, it doesn't really matter if your code is indented by four or 20 spaces or tabs or whatever. Square brackets in Wolfram language are parentheses in Python and just minor syntactic difference comments are inserted in a different way. Then some other minor differences, indices. Wolfram language is a one index language. So starts counting from one, starts counting zero from Python. White space does not matter in the Wolfram language, but this in Python. So here we have a function that is written in Python on the left and in Wolfram language on the right. So the point here. Well, first of all is to show you how similar the code looks. For example, here the parentheses are just square brackets in Wolfram language and some other minor differences. But the more important point of this example Here is that you can definitely write Wolfram language. And this is more for Python users who are writing or who are coming into Wolfram language as a new language. It's very tempting to start writing Wolfram language code as if you were writing code, and you certainly can. There's nothing in the Wolfram language that will stop you from doing that. It's not, it's not wrong, it's just not the best way. So here we have a function, it's not even a function, just an if statement and a for loop inside the statement in Python, which looks like pretty normal Python code that you might be able to write. There's nothing wrong with that. And the same thing is essentially copy pasted with the right syntax in Wolfram language. So both of these works and both of these check if a number is a prime and give you the result. But this is not. Wow, this is totally fine in Python. The code on the left, this is not what you would typically see on the right, it's not what you would typically see in Wolfram language code. So other than the minors in technical differences, there's, there's almost, I almost want to say a different way of thinking of people who write Wolfram language compared to people who write other languages. So here we can, we can see an example of something that is much more Wolframi, so to speak. This is the same function essentially as this one above, but it's written in a way that you would see more often in Wolfram language. So let's evaluate this function and talk about it very briefly. We are defining a function called PrimeCheck that takes an argument. When that argument is one, we're printing that one is not a prime, we're printing this is the argument is not a prime. Then we are defining the function again. So immediately we're overloading the function. Overloading functions. You don't need to know too much about it if you don't already. But it's something that's really common in Wolfram language. So here we're saying that PrimeCheck when the number is anything. So this blank here stands for any Wolfram language expression when it's anything other than one. Because when it's one, it matches this function. But when it's anything else, we are entering a different function. And here no need to go line by line, we're just checking whether the argument that's passed into the function is prime. And we're returning isprime if it is and it's not prime, if it's not. So this is a much more typical Wolfram language code way, which you can see immediately it's very different from the Python way. And again, I'm probably repeating myself at this point, but there's nothing wrong or right about those paradigms. It's just a different way of, a different way of thinking, in my opinion. We can test that the function works. We're passing 1, 1 is not a prime, we're passing 4, 1. So when we pass 1, by the way, just want to linger on this first line for a second. When we're passing one, what we are calling essentially is this function here. Our evaluation does not reach the second one because it already matches the argument structure of this one. So what we are getting back, one is not a prime comes from here, one is not a prime. When we pass other numbers like 4, 1, 7, we are getting into the overloaded function because 417 does not match the argument structure of this one, it goes into the second one and we get into this evaluation block here. Just a minor point, I think overloading functions is quite cool, it's quite a useful thing to do. But Anyway, we're passing 417 and goes into this block and it tells us it's not a prime, that it has factors, and 10 is not a prime either. But again, there's an even more important point here, is that, well, you can, of course you can write this function in a Wolframi way, but in many cases, and that's again especially useful for Python users where the instinct is okay, I need to do something, I'm going to write my own function for it. But with Wolfram language, if we go back to this graph, we have 6,300 functions. Now if you are trying to do something simple as checking a prime nine times out of 10, there is going to be a built in function for that. And in this case of course there is. It's called prime Q which takes one argument, which is the number it yields. True if it, if the argument is a prime number and false if it's not. So yeah, again I'll come to this when I, when I talk about my tips for Python users that. But the general point is that whenever you're trying to do something, before you start writing a whole function for it, have a quick look in the documentation. Even if it's something that in this case it's obvious, you can kind of guess that there would be a function that checks whether a number is a prime. But even, even if it's something less obvious, very often I've found that there actually is a built in function and I spent half an hour trying to cobble up something together. So it's always worth checking that. Again, this is part of kind of the different way that people who write Wolfram language code use. So this is the same point functions for everything. That shows how the number of functions in each release of Mathematica increases. And we are at 14, we are at 6,300. Again, same point that if you're trying to do something, there's a built in function. And kind of an additional argument for looking for the built in functions is that they are often much faster than the ones that you would write yourself, because they are. Well, they're written by people whose job is to write fast and efficient functions. So there are all sorts of optimizations going on under the hood, so to speak. We can just check quickly how long it takes our function to check whether a number is a prime and how long it takes the built in function to check whether something is a prime. So the scientific notation doesn't help, but the prime Q is the built in function is much quicker in doing that. Again, I already mentioned this. The documentation. When you, when you check some function in the documentation, there is usually at the bottom, oh, I don't have the offline documentation, but you can see maybe if I make this a bit bigger. You can see every time you check a function. So here we're checking prime Q. The documentation lists similar functions that you might be interested in. So in this case we get just more functions related to prime numbers. Another kind of distinctive feature of the Wolfram language is that it has access to a large kind of repository of curated data that you can use for all sorts of different things. So here there are some examples. And this is essentially WolframAlpha, which is probably for people who have not used Wolfram before. WolframAlpha is the one product that they are likely to know. So you probably know that WolframAlpha is very good at solving mathematical problems. And here are some mathematical problems that you can use directly, that you can solve directly from your rule from Mathematica. It could be just finding discontinuities, plotting things on a graph, but there are also much more general things like finding physical properties or materials. Here we have carbon steel. You get all sorts of different properties. Some popular culture things like Flintstones versus Simpsons gives you some information about each of those genealogical trees. Just very different things that you can think of are covered in the Wolfram knowledge base. So a minute ago I was mentioning how it's always worth checking if there is a function for something you're trying to do. This kind of a similar logic applies here in the sense that if you're trying to find some information, it's always worth checking whether it exists in the Wolfram knowledge base, because if it does, you can start using it immediately in your code. You don't have to go online and search for it and copy and paste it and then make sure it's in the right format or so on. So if I wanted to do something with carbon steel, with the density of carbon steel, I can immediately start, I'll take this code and I'll start writing more around it. So it's really helpful, depending on what you do, obviously, the way that you access the curated knowledge. Since 14.1 we have this really nice syntax here, which you can just type equals and then square brackets and then you can type your whatever, whatever you want to Query woo from Alpha4. So here I'm typing USA population. Hopefully it will give me some number and I can immediately start using that. But typically, I think before 14.1 we had this control and equals, which brings this natural language input in which you can type in the same thing and you get the same output. But I'm really happy about the new syntax. It's much nicer for me. But you can still do it. Again, the backwards compatibility point. I'm guessing that this freeform input is going to be supported for a long time to come. So if that's what you prefer, you can certainly use that. Yeah, I think that's all I have to say about the knowledge base. Again, the main point is that if you're trying to find some information, just do equal square brackets and try to find, try to query woo from Alpha and the knowledge base about the information you're trying to find. It may be the case that it's not there, but in many cases it will be. So it's always worth checking that. Okay, so let's get into some actual examples. So so far we've just been talking about the similarities and differences of each language and probably should have. Oh no, it's okay, so here we have some actual examples, but these examples here are just showing you again how to. How different things look in the different languages. So here we have some code in Python and then we have the same code in Wolfram language. I'm not going to spend, as I mentioned in the beginning, I'm not going to spend too much time on these examples. Well, this one specifically, this example is about calculus My background is in the social sciences. I'm not going to pretend to teach you calculus, but I can tell you what the main kind of point of this example is. To show you that for certain things you might find one language more useful than the other. So here, this example is about finding indefinite integrals. And this is what it would look like. I'm not going to go line by line with this code. This is what it will look like in Python. Oh, I haven't installed sympy, Just let me do this quickly. It's not that important anyway. But just to. Oh, it doesn't matter. This is what the, this is what the code looks like in Python if you want to find the indefinite integral of some expression. And this is what the same code looks like in Wolfram language. So again, for something like, for cases where you need symbolic representation of objects, you're going to find that Wolfram language is much more suitable for that task. And you can see that what took us, what's that, 20, 30 lines in Python is just two lines here in Wolfram language code. So obviously that's true for symbolical, for, for tasks that require symbolical representation, where they can make use of symbolical representation. It's not going to be true for every task, but that's the point of combining both languages. So if you're a Python user and you need to do something, you need to find an integral, you need to solve some differential equation, you need to do any bit of calculus, of course you can do that in Python, but you might find that it is much easier to do in the Wolfram language. So this is the point of this example, is just to get that point across. This example is more oriented towards Python and it talks about lambda functions which are, or anonymous functions, if you were to call them that, which are essentially unnamed. Well, as the name suggests, anonymous functions, which you can usually write in a single line of code that do a simple, single, simple thing. So here, let's get through this example in a bit more detail. So here we have three strings, red, blue and green, and we want to find the length of each string. What does the length of a string mean? Just how many letters there are in a string. So one way to do it is just to map the length function on each of those strings and, and save the output in a list and then print that list. So here, this should work. It does. You can also do that. Well, you have the. So you have the length of each string now, but if you want to get the product of the length of Each string, you can do that very easily with a lambda function. And lambda functions are actually, I mentioned that Python code is usually very readable. Lambda functions are probably not the best example of that, which is probably why they're here, now that I think about it. But essentially what's happening here is we have this. This is the same as the previous. The previous cell. And here is our lambda function, which is taking XY and it's multiplying it and there are no more elements left. And it's doing that on the result, which is this list of integers. So essentially we want to say 3 times 4 times 5, and we can do that in a single line using a lambda function. Let's evaluate that. It gives us 60. In Wolfram language, there is a similar concept to lambda functions, which is just using function. There are also pure functions, which I don't think they're mentioned here, but they would. I'll talk about pure functions anyway. They're really cool. But here, essentially what we are doing again is the same thing just in Wolfram language, we have the same three strings, we are getting their string length, we have this function and we are mapping the function on each of them and we are kind of multiplying the. We're getting the product of that list essentially with folder one. Cool thing about the Wolfram language, where we say everything is a symbolic expression. It means that you can often do things on different types of data without changing your code. So here we are multiplying the values of the different, essentially two images and we can use the same function that we use for getting the product of the length of the strings. And we can use that function on the product of the two images. Here's just some. The image data. This. Yeah. Okay, So I guess the point of the previous example was to show you that if you have a concept in one language, you can probably find a clause equivalent of that concept in the other language as well. So here, this one is a bit more interesting. It's about the functional programming and how. How you write functions in the. In the two languages. So in Python you will very often see this. Well, let's go through the syntax, actually. We're importing a library. We're defining a function called myfunc, which takes two arguments, X and C. I just, I'm just looking at the questions somebody's asking. Are there any more complex examples of using exchange information between Mathematica and Python? Yes, there are, and we will get to them in a bit. But that's a good question. So we have this function here in Python takes two arguments. And very often in Python you will see this branching logic. So we have an if statement conditions which checks whether something is true, and if it is, return something. If it's not, or if something else is true, it returns something else, and otherwise it returns an error. So this is just a very common kind of functional programming paradigm in Python. In Wolfram language, you can, you can definitely have this branching logic when you're writing functional, when you're writing functions. But again, the kind of. The more Wolframish way is to overload functions. So here again, this is the same function as the one in Python, but instead of using if statements and else ifs, we are using overloading. So this function holds when the argument is even and it's greater than one. This function holds when the argument is odd and is greater than one, and so on. So again, accomplishing the same things in slightly different ways. So here we have again, some code that. I don't think there's anything new about this example. Again, it's some code that is written in Python and you can do the same in the Wolfram language. This is just training on classifier. So let's. I'll cover this briefly. What this code here is doing is that it's taking a CSV file of. The CSV file contains information about the passengers on the Titanic. And you can see the head of this file here. I wonder if this is. Yeah, you can see the head of this file here. It contains information about the class of the passengers, their age, their gender, and whether they survived or died. So this is a common kind of data that's used in training classifiers or in machine learning, where you want to take this data when you want to create a model, and then you want to say, for any arbitrary input, let's say that you want to. You're curious about. If you are a passenger in second class and you are 35 years old and you're a male, what is the chance that you survive or not? So this is. The code here is way too long to go line by line. I want to get into the more interesting examples, but essentially it shows you how you can train that classifier in Python. So here, everything that you see is pure Python code. There is no from language code here. And then you have the final classifier and you test it again. Wolfram language, same thing, just slightly different paradigm, slightly shorter code. But the idea is the same. You're training a classifier and you're testing whether, under some conditions, whether it Whether the passenger, according to the model, whether a passenger having certain characteristics is likely to survive or die. So again, nothing too interesting here. Okay, so let's start actually using Python from Wolfram language environment. So we have been doing that, but here we're going to start passing some data back and forth. So let's see how that works. So just to cover the basics, the main way that you use Python code in Wolfram language, or one of the easiest way, I should say it's probably not the main way. One of the easiest way that you use Python called from Wolfram language is these external cells. So if you create a new cell, you're going to see that there's a special type of cell called external language cell, actually. And in, in an external language cell you have several languages that you can use. Python obviously are Julia, Octave, Java, Ruby, Node, JS and some other ones. But essentially once you have this external language cell, all you need to do is start typing your Python code or whichever language you choose here. And what wolframatomatica will do is it will try to locate the interpreter for that language on your machine. There might be a little setup involved in that, which is why some of my previous examples were not working as, as, as they should, because I haven't done that setup. But typically when you run something in Python, what Wolfram language will try to do is locate the interpreter, run that code and get the answer from the interpreter of Python and print it in Wolfram Mathematica. I hope that's not too complicated. It's a simple enough idea. What is happening here in my environment is that I haven't set up some of the libraries properly. One of the problems in Python, but actually we'll come to that. So that's, in a way that's, that's a good thing because I'll use it to show something. But any case, in any case, the, the basic idea is you have an external language cell and you're typing your external language in it and you get the response. So here we can, we can define a list which is range 10, so the numbers from, or in this case from 0 to 9, because Python is 0 indexed and we're printing that list, what you can do. So, so these, so this is fine if you're just writing, obviously, if you're just writing Python code, it's absolutely fine. And probably the easiest way to just create an external language cell and put your Python code in it and evaluate it. But obviously you will want to be combining Wolfram language code alongside Python code. So this is not going to. So the external cells are not going to allow you to do that. If I want to have some Wolfram language expression here, say map range 10 or map times to range 10, or something like that, this is not going to work. So to be clear, this is a Wolfram language expression. If it lives in a Python cell, Wolfram Mathematica will interpret it as a Python code and it's not going to work. So the way that you can use Wolfram language expressions alongside Python code is by using external evaluate. So external evaluate is a special function which does a very similar thing to the external language cells, but it allows you, well, first it allows you to specify some options for your external evaluator, And it also allows you to. Type your Python code as a string, which means that you can insert some Wolfram language expression. We'll see in the next example here. So let's see if this works. I'm not sure if my numpy. Okay, so Numpy works as. Works fine, but some libraries were not working. Okay, maybe it's actually a external language problem. Let me just test something really quickly. Is numpy going to work here? Yep, it works here. So, okay, I'm having some problems with the libraries. Okay, so I'm seeing a few questions. Can I call a library like Pytorch inside Mathematica? Certainly, yes. We don't have an example with Pytorch, but just the same way as we are calling Numpy here, you will do the same with Pytorch. It's not going to be faster if I, I know this is not what you're asking, but just to, just to mention it, it's not going to be faster if you call it from Mathematica because Mathematica is calling the same Python interpreter as your Python environment would be calling. But certainly if you want to do something in Mathematica and then call Pytorch from an external language cell or inside the external evaluate function, you can definitely do that. In fact, you can call any, you can call any, any library, any Python library from here, just fine. Not a good example because my Pandas I think wasn't working. But I think that's probably a problem with my machine because it was, it was working fine on my personal computer. But here we do the webinars from a different PC. So I haven't done the setup here, but usually it should work out of the box. Something is wrong with mine. But to answer your question, yes, you can call Pytorch. You can call any library. And then another question is, which version of Python can I specify? You can specify any, any Python version that you have on your machine. So if you have Python 2, for example, you can call that. I'll show you what the syntax looks like for that. Essentially it's going to be through external evaluate, I believe, but you can specify the specific Python version that you want to use, provided obviously that it's installed on your machine, you can point to its path directly and mathematically we'll call that evaluator. So if you want to use an older version of Python, if you want to use Python 2. Actually, I'm not sure if Python 2 works. When I was doing some testing on my personal PC, I called an, I think 3.9 and that worked fine. But we'll get to that. Just another question before I move on. How do type conversions work? Integer to double that sort of things? We will come to that. That's in one of the Jupyter notebooks, which I will definitely get to. But just to give you a short answer to that question. For simple data types like integers and strings and lists, the type conversions work automatically and they work in the way that you would expect them to work. So if you have a list in Python and you import it in Mathematica, it's going to be interpreted as a list in Mathematica and things like that. But we'll get into that a bit later on. So, okay, to get back to the notebook. Thank you for the questions. They're good questions. If you have any more, feel free to log them in and I will, I will get to them. Just to get back to the notebook, what we are doing here is we're importing NumPy, very popular library. We are using it to create a list of 10 integers and we're printing that list. You can see that. Again, just what I mentioned. Now this, obviously, this NUMPY list, I think it's actually, even if I'm not mistaken, it's not a normal Python list. I think numpy gives you something a bit different. But so this gets back to the type conversion question. For really common types, like, numpy is a really common library. So for really common types of data, the conversion is handled automatically. So here you would expect a numpy list to be interpreted as a list in the Wolfram language. And that's what happens when you get into slightly more complicated or slightly more obscure things. You will. Well, we'll get to that. So here we're again, we're using NumPy, this is a different function we're again calling External Evaluate with Python, just the default Python that Mathematica wants to find. We're importing numpy and we are getting a list here. Yep, I wonder what that error was. But it's not there now. So here is, is I think the first example in the notebook where we're combining actually Python code alongside Wolfram language code. So here important, numpy as NP is obviously Python code. We are defining list, which is a variable in our Python environment. We're calling a method on numpy. And here we're inserting our, our Wolfram language code. So this less than, greater than is is a shorthand for string join and here tostring then is is the Wolfram language code. And we are, we're essentially passing this Wolfram language expression, the evaluation of this Wolfram language expression to our Python code, because in External Evaluate, we are providing our Python code as a string. So what happens when we evaluate this is that this Wolfram language expression gets converted to the string and it's joined to the string that gets passed to Python. So this is a simple way in which we can combine both languages. One thing to keep in mind, and one good thing, is that when you assign something to a variable in Python, that assignment persists for your session. So as long as you don't quit your kernel, as long as you don't quit mathematical, obviously if you evaluate something here, if you assign something here and then you evaluate it, you are going to get that you can use that assignment later on in your code without having to assign it every time. So here we can quit the kernel and this is no longer going to be valid. It shouldn't be, because when we quit. So this, this quits the Wolfram language kernel. When we quit the kernel, the. The whole Python session disappears, it's cleared from memory, so then the assignments are no longer valid. Okay, so we're getting to transferring data. I do want to get to the jupyter notebook. So let me just have a quick look at. Yes, I think I'm going to skip this example because it does essentially, it does a very similar thing to what we did here, where we are using External Evaluate to evaluate some Python code and then later on we are inserting some Wolfram language expressions here. So the main point of this example is that if you want to combine Wolfram language expressions and Python code, You can do that using External Evaluate and then you have your string, and then you are essentially combining the string result of a Wolfram language expression alongside the normal string that you're passing to Python anyway, so there's nothing really fundamentally new that's happening here. So I want to get to the next. To the next section here. So we mentioned that for simple things, especially if you're passing strings from the Wolfram language too to Python, you can do that very easily. If you're passing simple Python objects back to from language such as lists and even things like numpy objects, They are going to be converted automatically and they're going to be converted in the way that you would expect them to. But obviously you're not always going to be dealing with simple objects. So sometimes you would have a library in Python that defines its own type of object. If you try to pass this back to the Wolfram language, it's not going to know what to do with it. So here we have a good example of how to deal with that. So here we are using a library called chess, which is what it sounds like. It creates an object which represents a chessboard, and then you can interact with that object programmatically, for example, to move figures and to get legal moves and things like that. So obviously this is a. It's an external library and it defines its own chess object, which is not recognized by the Wolfram language. So if you try to import it directly, it's not going to work. If you try to import a chess object into the Wolfram language, it's not going to work because it doesn't know how to convert that object into something which Wolfram language recognizes. But what you can do is you can store the object itself in a Wolfram language variable and you can pass code to Python from Wolfram Mathematica in order to interact with that object. This sounds a bit more complicated than it actually is. So let me just give you an example. Here we're starting an external session, so here we're starting a Python session, and here's where we can define essentially what this does, as you can see, is it's creating a virtual environment. And here you can pass, I believe in this Start external session function you can pass arguments for which Python version to use. I'll just have a very quick look just to make sure I'm not making this up. Yeah, so here's the. Here's the way in which you can pass different versions of Python using a list of associations where version goes to whichever version you want. I believe this should also be able to take a path as an argument. So if you have Python whatever, 3.9 installed somewhere you can use that path directly, just so Mathematica knows where to locate that evaluator. Or maybe it's in Python runtime. Let me just have a quick look. Version three runtime. Yes, I believe it will be in the runtime environment. So again, you can pass 3.11, you can pass a path as well. And from mathematical, try to find that. But anyway, let's not get distracted. Here we are. So what we hear, what we do here is we are starting a Python session. We are saying that we're going to need the chess library. If that's not already installed, Wolfram will try to get it on Mathematica's behalf. That doesn't always work like you would expect, so I would recommend, when you're using external libraries, just install them from Pip and then when Mathematica queries the Python interpreter, they're going to be there. I think it's a more reliable way of doing it. Maybe it's fine in some cases, but I found it a bit buggy when, when I'm relying on Mathematica to install some libraries. But in any case, we have this Python session here. We are importing Jess. So what session prologue means here? It's a. It's an argument that says, before the session start, evaluate this Python code. So we're evaluating import chess and we're defining an image of the chessboard. So now that we have a Python session, we can interact with it, we can call methods on it, we can hold it in variables, and so on. So this session is going to persist again as long as your Wolfram language Wolfram Mathematica session persists. So you can use it, you don't need to define it every time, and so on. So here, what we're doing is we are calling this session here, where we imported the chess library and we're saying, okay, I want a board now again. So if you are doing this in a normal external language cell, a chess board, chess.board, parenthesis doesn't actually mean anything to Wolfram language, so you wouldn't be able to import it. But if you hold it in a variable, you're able to interact with it on Python side. So here we are just interacting with. We're holding this in a variable and we can call methods on that variable. So we're holding it in board and we say, okay, we want to get some full move number, we want to push some figure to a certain. To a certain position on the chessboard. And typically if you do this in Python, you will see that every time you call one of these methods, something happens on the chessboard. But here from woofra Mathematica, we are just telling Python to do things on that object without us necessarily seeing what that object becomes, because we have no way of representing it here. But here you can, once you have done everything, you can import the board as an image. So this is in Wolfram language. This is just a simple image. There is no. If you are doing this in Python, this chess board would actually be an object that you can interact with. But if you just want to visualize the current state of the board, you can just import it here and then you can do other things with the with that object on Python site. So here we're getting the legal moves. Again, you can notice that again, those legal moves would be actual Python objects. Here we're just representing them as an external object. We're not really representing them as anything because they have these methods here that we don't know what to do with and so on. Okay, so, oh, one good practice is when you are defining sessions like this, it's good practice to delete them after you're done with them. I think when you quit mathematically, these sessions should be deleted automatically. But it's always good to do it yourself just in case you don't want kind of lonely processes hanging around in your system. Okay, so I think from Wolfram Mathematica side, this is all I want to talk about because I want to get to the Jupyter notebooks. Yeah, so we're going to do this in Jupyter. The main point here, I'll just summarize briefly before I move to the Jupyter notebooks. The main way you can use Python from Wolfram Mathematica is via an external language cell. If you're just writing Python code, or if you want to combine Python code with Wolfram language code, you can use external evaluate for simple. So, for example, in this case we are just passing a string that is generated by Wolfram language code and is then passed to Python. And if you're doing something in Python where you have complicated objects that you know, Wolfram Mathematica is not going to have a way to represent. You can start an external session and then you can define. You can store the objects of that session in a variable and then you can interact on them without necessarily bringing them back to Mathematica. And yes, just to go back to the question about different Python versions, when you're starting an external session, this is where you'll be able to define which Python version you're using, I would advise you to have a look at the documentation. I don't remember it off the top of my head, but this seems the way to do it. Okay, so let's get into the. Get into the Jupyter notebooks. So so far we have been talking about using Python from Wolfram Mathematica. Now we are going to switch slightly and we're going to do it the other way around. We're going to use the Wolfram evaluator from Python environment, such as this Jupyter notebook here that I've opened in VS Code. Again, this is, again, it's a notebook. It's the same idea as the over mathematical notebooks. You have a combination of texts and code and inputs and outputs. But here typically you would be writing Python code or Julia code or whatever. In this case we're doing Python. So I'm going to show you how as well as writing Python code, which you can do anyway. I'll show you how you can write Wolfram language code and get the results from the Wolfram language evaluator in your Jupyter environment. So the first thing we would need to do is we are going to need this library called Wolfram. I believe it's called Wolfram Client. Let me just double check because I'm not good with names. Yes, it is called Wolfram Client. So you're going to need this library Wolfram Client and this library will allow you, this library will allow you to use both default Wolfram mathematical functions like range and select and reverse, just functions that are available in just your base Wolfram language. And you're going to have this method called Wolfram Language or WL Expression, which is going to let you evaluate custom defined functions. So in many cases you would want to write your own Wolfram language function and use it from Python. So both of these methods are available in Wolfram Client. So all you need to do is install this first, as you saw me do. Well, I already have it installed, but typically you just install it just like any other library in Python through PIP or however you manage your packages. Once you have it, all you have to do is import it and then you can start, you can start using Wolfram language code. So let's evaluate this and let's, let's see what it does. Okay, so here we are. Oh, we're importing the Wolfram client package and we are defining some Wolfram language functions. So at this point we're not actually evaluating anything, we're just saying that in the Variable X We want to store the following Wolfram language expression. We want to use the base Wolfram language select function and we're going to pass it as arguments. The base Wolfram language range 10 and the base Wolfram language prank queue. So here we're just defining the function. So this evaluates. So we're printing X. So you can see that this is just a normal Wolfram language code for x select range 10/ Prime q to say that in plain English we are from the integers of 1 to 10, we are selecting the ones that are primes. Again, we are just defining the function and then once we have it in a variable, we can use it anywhere in our Python environment. As we're going to see in a bit, we're defining a few functions. So this one is selecting the primes from the integers of 1 to 10. The second one is reversing the order of the integers of 1 to 10. And you can see what the kind of Python syntax looks like here. Again, we're using WL to say that we're going to use a base Wolfram language function. Same for the range. And then the final one is just getting now, which is going to give us, I believe, the date and time at the point of evaluation. So we are assigning these Woofram language functions to our Python variables. So X, Y and Z in this case are just normal Python variables that you can then use. So as I mentioned, if you, if you are using a base function, you can just preface it with this wl dot syntax. But if you are, if you want to store some Wolfram language expression that doesn't have a name. So in this case, this is a pure function. It doesn't really have a name, it's just something that the user defined themselves. You just need to preface it with Wolfram language expression or WL expr. Other than that, it works in the same way. You can still store it in a variable, you can still evaluate it later on and so on. And this list, 1, 2, 3 is just the arguments that we're passing to this, to this pure function. Again, at this point we're just, we're just getting the function, we're not actually evaluating it. So let's actually start evaluating things. The first thing you will need to do is you need to start a session just in the same way. Oh, I'm just seeing that the font size is a bit smaller. Apologies for that. I should have. That's always my problem. I forget about that. I hope this is a bit easier to see. You can have you have access to these jupyter notebooks? Anyway, but apologies for that, I should have noticed it earlier. Essentially again, what we're doing here is defining Wolfram language functions in Python. Okay, so let's start evaluating to to actually evaluate these expressions that we created here, we need to have a Wolfram language session. This is the same as a kernel in Wolfram Mathematica. Whenever you want to evaluate something, you can write code in Wolfram mathematically without starting any kernels, but once you actually want to evaluate them, you need to start the kernel. And this is the same idea here, it's just that we are storing this kernel in a session. This is just your typical object oriented goodness or badness, depending on your feeling about object oriented programming. But in any case, you're storing this session here and the way you create the session is using this Wolfram language session function. This comes from the same library as before, Wolfram client, it just comes from a different part of that. So the argument you want to pass to from language session, again it should work by default even if you leave this empty. Python should be able to find where your rule from automatic is installed. But in my case on this PC, on my personal PC, it worked without providing anything. On this pci, I explicitly had to provide the path for it to work. So keep that in mind, Try it without any arguments first and then if that doesn't work, you can just copy and paste the actual path to your woo from Mathematic installation or to the kernel in this case. Ok, so what we're doing here is we are starting a session. So this line, all it does is it starts a session and then once we want to evaluate something in that session, we're calling the session and we're calling this evaluate method on it and we are passing it whatever it is that we want to evaluate. So in this case we want to evaluate X and X just to go back, X is this select range 10 prime q. So we're selecting the primes from the first 10 numbers and they are, you can see here you have more information about the data types. But I believe actually if you just store it in a variable you will be, you will get just the plain output. Just testing this quickly. Yeah, so if you just want to get a plain output, make sure to store the evaluate result in a variable and then do whatever you want with that variable. So here we were getting the primes from the first 10 integers and they're 2, 3, 5 and 7. And then once you have this, once you have this In a Python variable, you can do everything you want. You can apply any piece of Python code on it that you want. Here we're doing the same for the other two functions. The function, the Wolfram language function that we stored in the Y variable was just reversing the first 10 integers and you can see that again you're getting this array, but this time they're reversed and Z was. Oh yeah, it was null. So the Wolfram language function, now, which gives you a date object which contains year, month, day, hour, minutes and so on. So this is just a very simple way in which you can, you can start using Wolfram language directly from your Python environment. All you need to do is, well, first you need this library. You define your expressions using WL and WL expression, depending on whether they're built in or custom, and then you create a session and then you, you call the evaluate method on the session with the Wolfram language expression. I hope that's clear. It's a bit difficult to explain, but if you, you have access to these notebooks, if you're interested, just have a, have a little play yourself and you'll see that it's, it's quite intuitive, actually. It's just a bit awkward to explain. And again, we are coming back to the question about data conversion, which is an important question that somebody asked previously. As we saw above, when we have some simple data types like lists and integers, they're automatically converted into Python formats. So when we had this list here, when we stored it into a variable, we printed that variable. And this is essentially a Python list. There is nothing woo from languagey about this list. You can do anything you want on this Python list, but when you have more complicated objects, that's not going to be the case. So even when you have something like an image, there are many different types of images that all mean different things in Wolfram language, let's say. So when you create a plot, that's obviously an image, but there are interactive elements in the plot, so the way that they are represented in the Wolfram language is going to be different the way that they're represented in Python. So when you have these more complicated objects, you're not going to be able to just assign them to a variable and then call the variable and get them. Technically speaking, you can, you can define your own encoders and decoders. So you can, if you have some complicated expression in the Wolfram language, you can serialize it into something like binary and then write a custom encoder, deserialize that in Python, I would not recommend that simply because they're almost always. There's a much simpler way to do it. So here we're going to look at. We're going to create some image, I believe, and we're going to look at. So typically when we create an image, we will not be able to directly import it back into our Python environment, but we can export it from, from Wolfram language as a PNG file, as just a normal photo file, and then we can import that image from Python because you could write your custom encoder, but both languages already understand png. So this is really a much more simple and intuitive way to do it. So let's actually evaluate this while it's evaluating. Let's see what it's doing. We're creating a bar chart. We have some labels, we're defining a path and we are exporting our bar chart into this path and we're calling this bar chart bar chart 1 PNG. So here we're just defining the expressions and you can see at the end we're calling our session, which we started here we're calling our session and we're telling our session, evaluate this thing, which would be this thing, PNG export. The previous ones are just variables. So now once we have exported that image, we can import it just as a normal image. It is no longer just trying to zoom out a little bit. But in that case, can I. Oh, yeah. So we are importing this image just like we would import any normal image from Python. We no longer see this as a Wolfram language language expression. So in many cases this will be again, depending on what you work with. If you work with images, the easiest way is to just export them to PNG or SVG or JPG or whatever makes sense for that image and then import them into Python. So even though they're not technically supported in the technology stack that allows you to serialize different types of, of objects from one language to the other, you can always find a common format that both languages understand. And that's going to be the simplest way to do it. Again, we are doing the same thing. We are exporting some image varchar2png. In this case, we're evaluating this expression and then from Python. So these. Oh, this is Wolfram language code. We're just storing it in a variable because then we want to say to our session, please evaluate this Wolfram language code. Then the last line is Python code. So we're saying image open. We're using. Where do I import? Yes, we're using Pill, which is a Python library for dealing with images. So we are saying now with this final line, we are already in Python and we're saying just import this image briefly or briefly mention functions, because they work in a similar way to just normal from language expressions. But what's different with functions is you can wrap them in Python functions. So you can see the syntax. Here is your normal Python function. Define the function, name the arguments, and then you assign something to it. So when you are returning something from a Python function, that something could be a Wolfram language evaluation. So in this case, again, we are defining some Wolfram language function. So everything below this is now Wolfram language code or Wolfram language expressions. And here what we are doing is we are getting some value. We're getting the population density of a country, so we're dividing the population by the area. And the argument that we're going to provide this function is the country. So we can test this, we can evaluate it. So we have a Python function holding a Wolfram language expression. So when we pass, we use this as a normal Python function as far as we're concerned, and we're passing it some country, then what this function does is it evaluates this Wolfram language expression and gives us back the result. So we'll get the population density. Here, France, we can test with something else just to make sure our function is working. Again, a very similar idea. Once you know how to define expressions for variables or just for simple expressions, writing functions is basically the same thing. Again, similar idea. Now, instead of defining Python functions, you can call it directly from session evaluate. I mean, the only interesting thing to mention here is this notation of the three apostrophes or quotation marks, I'm not sure which one. What this does is it allows you to have a multi line code. So you can see that this is a single piece of code, but it's spread across multiple lines. Whenever you want to write multi line code, just include these three apostrophes and then close them at the end of your code. Other than that, instead of assigning it to a Python function, you're just directly evaluating it from the session. Nothing interesting here, but let's test it just to make sure it works. Yeah, you can use. Again, this is similar. I want to get to more interesting things. Again, the thing to mention again, just like we Just like I mentioned about Python session remove from Mathematica, when you are done with them, it's always good practice to terminate them. So the syntax for doing that is session terminate Here. Okay, so let's see. We have a few other Jupyter notebooks that are quite interesting sometime. Let me see if this was the one. Oh, yeah, this is about custom encoders, if you're interested in. Was this about custom encoders? Yeah, this is just discussing the things that we've already talked about. Lists, digs. This is actually answering the question that I got a few minutes ago about how exactly are the different types of objects mapped out? So list sets and dicts, these are Python types, are serialized into Wolfram language lists generators, Iterators are also evaluated into lists. Numerical types are serialized into their equivalent in the Wolfram language. So floats would be floats, integers would be integers. I don't know if they have doubles, but date, time, date time are serialized into date object, time object. So this is just a bit more specific on how these objects are passed from one language to the other. Other than these basic objects, there are some popular libraries that are supported out of the box. So if you're using Pill, we mentioned that Pill was the library for dealing with images in Python using NumPy. We already saw that NumPy. NumPy objects automatically got converted into a nice list in the Wolfram language. Pandas as well, if you're dealing with data frames and data sets and so on. So again, nothing new here. We have two more notebooks that I don't really have time to get into, but I think we covered the basic things in the Jupyter notebooks. This one, the third one, called Cloud Connections, is just, I believe it's. It talks about cloud objects. So as well as using the Wolfram language evaluator on your system, you can also use the cloud version, but you need to set up authentication keys and that's just too long to do on the spot if you're interested. All the information is nicely documented here, but basically the idea is the same. The only difference is that instead of having your local evaluator do the evaluation, you're having the cloud evaluator, which basically means move from language servers. And then the last Jupyter notebook is some more extended examples into combining both languages. So here it's using Pandas and Empires is dealing with some data frames. It's all good stuff, but it's basically everything that we have covered so far. It's just a bit more extended to give you some actual use cases. We're importing some countries and we're getting from Wolfram language side, we're getting data on their population. And I believe in the end we're just generating a plot. But in any case. Oh yeah, I think one of these evaluation was taking a long time when I tested it. Oh yeah. The end result is just a nice graph. But in principle, there is nothing new in this notebook that we haven't already talked about. It's just using that Wolfram client library to generate expressions and to evaluate them and to pass data back and forth between Wolfram language and Python. I just have a few minutes for my tips for Python users. Now, let me make this a bit bigger. I realized immediately this time I'm improving. So what I wanted to briefly touch on here is some different paradigms that Python users might be familiar with and might be comfortable with and to basically give them the functional equivalence of those paradigms in the Wolfram language. So the first example I have is list comprehensions and Pure functions. List comprehensions are obviously not. Obviously they are a Python paradigm, I would call them that. Are. They're essentially the way I think about them is that they're simply for loops. So if you have this simple for loop here, this is Python syntax for something in something else, do something. So here, for each number in range 10, print that number. So this is just a typical traditional for loop. But if you want to write that as a list comprehension, you would have this notation here, which is you're defining a list and then you're having essentially this for loop in a single line we are printing X. Oh, this should be print X for X in range then. So again, this is. If you're primarily a Wolfram language user, that's probably not going to make a lot of sense to you. If you're a Python user, you've probably seen these list comprehensions. For me personally, I really like list comprehensions. I mean, they're more succinct and I believe they're also faster than normal for loops. So how do you do. What is the equivalent of list comprehensions in Wolfram language? Normally you have, you. You also have for loops in the Wolfram language, but I don't think you see anyone in any Wolfram language code doing this. More typically you would see somebody using table to generate some list of elements. But if we're talking something that is. If we're talking about something that is similar to list comprehensions in Pythons. What I find myself using very often is pure functions with map. So you can see. Let me delete all of this. Actually, we don't care about these. We just want to look at the list comprehension and the pure function. So you can see kind of the similarities over. Well, the way the syntax works in Wolfram language, pure functions is you're defining a slot. This ampersand says it's a pure function. This notation here, the slash and at is map. And then essentially you're mapping the left side of the pure function to whatever comes after map. So here we're saying every number from the range of 1 to 10. So if you're a Python user and you're longing for list comprehensions, I think, well, definitely don't use for loops. But even things like map and table are a bit further away from list comprehensions than pure functions. So pure functions will be your equivalent for that Briefly go over. Well, I'm not even going to go through the examples, I'll just mention what they are because I'm running out of time. So external libraries based functions. I already talked about this. If as a Python user you're used to doing something using an external library, you're going to find in many cases that, that they're base functions in Wolfram Mathematica to do the same. F strings, another paradigm in Python, which I actually didn't find a very close equivalent of that into Wolfram language. But string form I guess is the closest one you'll get to that. So if you like F strings, definitely check out string forms. Regular expressions, very useful, very commonly used in Python for kind of searching for patterns in strings. You can actually use regular expressions in a similar note, but similar enough way in Wolfram language. But in Wolfram language you also have these string expressions, which to me, I'm still not completely used to them, but I do find them nice and probably more readable than regular expressions. So it's not a very high bar. And finally, entities. Normally when you're dealing with something in Python it's either a string or an integer, and then you have to define the custom type to actually represent an entity. So here I have an example of dates. Obviously you use date, time, you're converting the string to a certain format and then you can do some computation with those objects. In Wolfram language, there's this powerful concept of entities. So again we're going back to our natural language syntax, the equals and square brackets. So just input. You don't need to tell Wolfram language these are dates, it's going to interpret them as dates and then you can immediately do computation on them. So these are just. Yeah, you have this document. I haven't. It's not very extensive, but you can have a quick look, especially if you're a Python user, I'm hoping you'll find some of these useful. And with that, I'm a few minutes over, but I think that was it. I managed roughly to get through everything I wanted to get through. So. Yeah, thank you. And if you have any questions, now would be the time to ask. I'll just have. In the meantime, I'll have a. Have a quick look if I have missed any, because we made. Somebody's asking how I can get certificate of attendance. I'm not sure. I think. Yeah, I think you have to email Wolfram Research Europe and ask how to get that. I'm not sure our marketing team takes care of that. So. Okay, we have more questions. So, to use Wolfram Session to evaluate, you need to have a valid license of Wolfram Mathematica and an installation on your computer. Correct. Having installed Wolfram Client on your Python environment does not seem sufficient. Yes, so that is correct. I probably should have mentioned that. Thank you for the question. Yes, you need to have a. Well, you either need to have a Wolfram Mathematica installation already on your computer, in which case, let me go back to the Jupyter notebook, in which case you can just point. Yeah, so you start the session and you point to your local installation. So that's the typical way you would need. But yes, you need a license and you need an installation. Another option, which I didn't really get to was using the Cloud evaluator. The process is kind of similar, but obviously instead of pointing to a local installation, you generate. You generate authentication keys and you input them in your. On the Python side, just to, again, to make sure that you're using a licensed version of Wolfram Mathematica and so on. But those are the two main ways. But in both of them, yes, you need to have a license of Wolfram Mathematica. I should have mentioned that. So thanks for the question. Okay, so we have another question. When integrating Wolfram language with Python, what are the most common limitations encountered when trying to evaluate Wolfram language expressions from Python? And how can we overcome issues related to conversion of complex data types, such as data objects or graphics, between the two languages? That's a great question. And that's. I mean, to answer the first part of the question, what are the most common limitations? I mean, you already touched on them. The most common limitation is converting objects, especially complicated objects, between one language and another. In terms of how to overcome that limitation, I mean, we had an example with an image in. In the specific example, when you're dealing with images that are not able to be converted. The, the way to work around that is to export an image from one language. And both Python and Wolfram language are very capable of just exporting an image to a, to an image file and then importing it from the other from the other language. The more technical answer is would be writing your own encoder and decoder. There is documentation for that in the third. Oh, no, in the second Jupyter notebook. Again, unless you really need to do that for some reason, I would advise against it because there's always going to be an easier way by exporting whatever you want to an external file and importing it back to wolframatomatica. I hope that answers your question. Is this available online? Is another question. Yes, it is. I mean, you can. As long as one of your environments can find the other environment, then you can do it. You can do it from anywhere you want. I mean, the Jupyter environment can be run on a local host, so that's kind of online, but it still has access to your file system, so you can point it to your local installation. I think even if it's completely online, you can use the cloud, provided that you generate the authentication keys and go through that process. But yeah, there's no, there's nothing stopping you from doing that completely online, I think if that's what you would like to do. In the context of using Wolfram Cloud for remote evaluations, what specific aspects should be considered optimized definition and execution of functions when they're evaluated in the cloud, considering the differences in persistence and performance compared to local evaluation? Yeah, a very good question. I'm not too intimately familiar with the limitations of the cloud simply because I don't really use the cloud myself. But just off the top of my head, well, one thing you touched on, you're going to have issues with persistence and they are touched in this in, in the third Jupyter notebook that I didn't get a chance to, to go through. But typically what, what you would, what you would do is you would have a different way of saving assignments to your variable to make sure that they persist. Because if you evaluate something in the cloud in one line and then you come back, so this, this evaluation happens on a server in Wolfram. Then you come back 10 minutes, you evaluate something else, even if it's the same notebook, it may get evaluated on another computer. So that means that you don't always have access to the same thing. So that's an additional consideration that you have to keep in mind. I wish I could give you more details about this, but I actually don't really know. There are some things covered in the Notebook, so you can have a look at that, but typically that's more a question about using the Cloud, because the same limitations, even if you're just using normal Wolfram language, you have the same limitations as if you're using it from Python. So I wish I could give you a better answer for that, but. Okay, just checking the questions from different places. I think that is everything. Sorry, I ran a few minutes over. I hope that was interesting and. Yeah, thank you. Thanks for attending. I hope that was useful.
More from Guy

Cheryl Strauss Einhorn | The Human Edge: Smarter Decisions in the Age of AI | Talks at Google

Rachel Glennerster | Innovation and the Center for Global Development | Talks at Google

Rebecca Winthrop | Rethinking the Purpose of Education in the Age of AI | Talks at Google

Dr. Robert Wachter | A Giant Leap: How AI Is Transforming Healthcare... | Talks at Google






