Це відео відноситься до openHPI курсу Künstliche Intelligenz und Maschinelles Lernen in der Praxis. Бажаєте побачити більше?
An error occurred while loading the video player, or it takes a long time to initialize. You can try clearing your browser cache. Please try again later and contact the helpdesk if the problem persists.
Прокрутити до поточної позиції
- 00:00After we just had the Basics in Python,
- 00:05now we want to carry on the whole thing build a small pocket calculator and build on it
- 00:10a few more concepts from Python.
- 00:15Yes, and if we think now to create an interactive Python calculator
- 00:19then we can build Think about what we need for that.
- 00:22And just write down it's ultimately a function.
- 00:28to query the user at all.
- 00:31So, for example, what a user charges and that's what Python actually offers
- 00:36again a nice standard functions, that we can use. We would then like to
- 00:40Python function with which we add numbers can. In fact, we already have
- 00:44from the last video. Then a Python function to report the result
- 00:48and finally, something we can do with these sequences.
- 00:54Ahead of that, just here again as a small disclaimer, you can
- 00:59Pocket calculator in any way and I'm sure that would be the case.
- 01:03Get a much faster or nicer solution than I'm going to show you.
- 01:09I just want to focus on that. show the whole thing clearly
- 01:14and show you an example.
- 01:17And you are cordially invited to join this Pocket calculator may be used in other ways
- 01:21or to suggest how it could be done differently.
- 01:24I'm sure there is There's lots of possibilities.
- 01:29Exactly when we Now think about how do we start?
- 01:32Then, we had just said, Let's do it with a user query.
- 01:36For this, Python has a It's pretty standard.
- 01:41So again, a machine here more or less, with which we use user data
- 01:46, which means input.
- 01:48You can add a variable or Enter the constant with which we specify
- 01:56what to ask the user.
- 01:58That is, if we do now simply execute it
- 02:00here is a user input that , which asks what should be the first number.
- 02:06And now we can go into this window. simple, yes, the value we want
- 02:11type, for example 10, and do that.
- 02:14And because I'm here right now This includes renewed demand,
- 02:17what should be the second number will be directly the next user query,
- 02:21and ask what the second number should be. I'll take 20.
- 02:26Yes, and now have fully interactive on the location without the source code changing
- 02:32, but simply by running two variables, namely first number and second number.
- 02:38Now we can try this one. both numbers in our two numbers
- 02:44machine, that is, the function.
- 02:46When we see the execution We have a very strange result.
- 02:49In fact, I add 10 and 20 and the result is 1020.
- 02:53That's very confusing. Because 10 and 20, that would be 30.
- 02:58And we have to get to the point We're thinking, where is that coming from?
- 03:02We will now debug our program.
- 03:05And that's a very, very important thing in programming that you understand why
- 03:10the program does not run as you would , but has a different behavior.
- 03:16And now there's a lot of possibilities how to debug.
- 03:21And we make it very easy for us to do that. Let's see what data types have
- 03:26our variables. And we are using it again type and print for and see that the variables,
- 03:33which we have set via input, of type string are. And now it makes sense again.
- 03:38Strings we had learned earlier, be e.g. in the case of an addition, simply
- 03:42with each other. That is, this Values are written one after the other and because we
- 03:4910 and 20, the and 1020 comes out.
- 03:54This can be totally confusing at this point just because 10 and 20 are too
- 04:01concatenate, 1020, that is It's really like a bill.
- 04:04Yes, but it is not a string that you immediately recognize, but simply
- 04:08a number by sight.
- 04:11That's why debugging can be done in part sometimes difficult and time consuming.
- 04:16But we know now we have strings here and we need to take a precaution to ensure that we
- 04:22then we can process our data.
- 04:24And here's where it's easy to do. we can assume that
- 04:29, we assume that our input data are always numerical values,
- 04:36but are still interpreted as a string, then we can do that with float, which is the wrapping
- 04:43with float, so almost here again more or less a function,
- 04:47and from that the Really get numerical values.
- 04:50That is, if we go through this now, we see. that our data types are then float,
- 04:55so decimal numbers. And with it, we then return to normal work.
- 04:59That is, we run the same function again, but now with the
- 05:05converted variables, and see me add 10 and 20, the result is now 30.
- 05:10So we seem to be there problem.
- 05:15Right, now the second question, we have calculates the result, now we want to
- 05:22the result may be simple should be shown to our user.
- 05:26And that's where we might use it. maybe just a new print for.
- 05:29That is, the constant "give the result is" and then add the sum
- 05:36and can then easily display. So here, very, very simple.
- 05:41And now, finally, we want that. simply and ultimately combine
- 05:47and into a function. And we can do that for example.
- 05:53as function "interactive summing" . Do not take arguments because
- 05:59the arguments ultimately about our input function, i.e.
- 06:05which consult the user, what the data is supposed to be.
- 06:08Then calculate the Pay with each other and give the result.
- 06:14Now I just said that you can get here on to write out Python functionalities
- 06:20could. You could do that, of course. That means you could take a
- 06:25Python function and enter the now, Just like we did here.
- 06:30And so we could do it downstairs as well.
- 06:32All different possibilities. We have it now on the spot, so that we can
- 06:39task, i.e. the user query of the data, did not write a function itself, but
- 06:45have it only here, for calculation then but use a function again, that is
- 06:50Encapsulate functions. And for the last Execution again a very simple statement
- 06:56have. If we now run, we've redefined our machines.
- 07:01"interactive adding", and can be run normally.
- 07:04That is, if we use them now, we ask our user,
- 07:08What should be the first number? 10th What's the second number? 20th
- 07:13Get the statement that the two numbers and get the result 30.
- 07:18Now we've looked at functions.
- 07:22Now we would like to have another look to what if-then rules are.
- 07:27And that too is quite a important concept in programming.
- 07:31That's this if-then-else. mapping logics based on the situation of the data
- 07:38for example, Perform calculations.
- 07:40So, with our calculator. For example, we can only add up.
- 07:45But we might also want to subtract and we can do that by making sure that we can
- 07:51for example, another argument.
- 07:56So "is_addition", that's maybe easy a Boolean, that is, a data type,
- 08:00who says whether a condition is right or not. And then query if the value is correct,
- 08:06so if we are_addition as a scenario , we follow this logic.
- 08:13Otherwise, i.e. in all other In cases, let's do this logic.
- 08:19So for example Addition versus subtraction.
- 08:23In the end, however, depending on whether we have addition or subtraction now.
- 08:30the result to the user and then return it.
- 08:35So now it's going to be kind of the same twice function, with different
- 08:42arguments, and once add and subtract once.
- 08:49Now I had the previous video tells you that there are other data types.
- 08:55And a very important data type are the so-called lists.
- 09:00The principle is completely simple. We have now not just a value that we put into a variable
- 09:06but potentially simple multiple names, such as multiple names.
- 09:10And it's very easy in Python to do that by square brackets and a list of them
- 09:16. And now I can do several strings e.g. to a variable, i.e.
- 09:23into a list, for example. I'll do that too and connect it directly to the next
- 09:29Concept, namely with the so-called grinding.
- 09:30And that's very interesting when you think about logic. repeatedly wants to execute on different
- 09:38data, in so far as, for example, has multiple data points, and for them all
- 09:43to execute a certain logic.
- 09:44So for example, if I have a have a list and greet each person from that list
- 09:50would like to. Then I can say that for everyone single name, so this will be dynamic
- 09:57generates a variable, executes the following logic should be used. So for every name in the
- 10:03List the statement "Hello, my name".
- 10:08I can just do that here. I see now I have generated four issues each
- 10:16with the prefix "Hello, my name" and then according to the names that occur.
- 10:25Then there is the last data type These are the so-called key value structures.
- 10:31That sounds much better in English. These are the so-called Dictionaries.
- 10:35with which we can create beautiful structures for example can build up into occurrences, i.e. how often
- 10:44for example, certain values occur and thus, for example, statistics
- 10:49or similar things.
- 10:51So let's imagine. have a multi-color list,
- 10:55which may occur more than once.
- 10:57We want to count through now, but not per Make a hand, but with a Python program.
- 11:01Then we could create something called a dictionary. where we have the values as
- 11:07key and as value 0 each. And then through iterate our list and the value of the
- 11:16Dictionaries a corresponding Count color up by one.
- 11:20If we do now, we can easily see how often the individual values occur, i.e.
- 11:26List up here comes Roth for example three times, blue four times and so on.
- 11:33Yes, so much for the essential basics, that we will need for this course.
- 11:40Now there's a very important concept. These are the so-called programming libraries.
- 11:45That is, if you use code from others developers want to use what is easy
- 11:51is very often the case in development. And because it's such an important concept.
- 11:55Let's see that again in the next video.
Щоб увімкнути запис, виберіть мову в меню налаштувань відео.
Про це відео
- Auf GitHub haben wir alle Materialien für die praktischen Einheiten zusammengefasst und für Sie aufbereitet.