{"id":85,"date":"2021-05-06T19:56:53","date_gmt":"2021-05-06T19:56:53","guid":{"rendered":"http:\/\/juthon.com\/?p=85"},"modified":"2021-05-22T14:35:55","modified_gmt":"2021-05-22T14:35:55","slug":"passage-2-hello-world","status":"publish","type":"post","link":"http:\/\/juthon.com\/index.php\/2021\/05\/06\/passage-2-hello-world\/","title":{"rendered":"Passage 2: Hello&nbsp;World"},"content":{"rendered":"\n<p>Welcome to the second passage of our Python programming language series. We already have Python and IDLE installed on our computer. We can&nbsp;act!<\/p>\n\n\n\n<p>If you don't remember what IDLE is, take a look at the previous post.<\/p>\n\n\n\n<p><a href=\"#Odcinek-2\" data-type=\"internal\" data-id=\"#Odcinek-2\">Jump to Polish version<\/a><\/p>\n\n\n\n<p>We will take our first steps in the IDLE main window called \"shell\". The shell is an interactive window that is great for writing very short programs (scripts) for purely training purposes. And that's where we start: writing mini-scripts in shell. In the future, when we want to write slightly more elaborate scripts, we will do so in Python files.<\/p>\n\n\n\n<p>There is such a superstition in computer science circles that you will not get lucky in programming if&nbsp;you don't write your first program and have the words Hello World! generated on the screen.<\/p>\n\n\n\n<p>If so, let's do it! The text that Python has to \"print\" (on the screen) for you is technically called a string. We must (obligatorily) mark the beginning and the end of the string with quotation marks (or if you prefer: with an apostrophe):<\/p>\n\n\n\n<p>\"Hello World!\" &nbsp;or &nbsp;'Hello World!'<\/p>\n\n\n\n<p>Now it's time to get to know the first Python \"syntax\" command. The first command we'll know is print(). Note the parentheses used after the word print. Within these parentheses you put the content you want to&nbsp;print.<\/p>\n\n\n\n<p>Therefore:<\/p>\n\n\n\n<p>open IDLE<\/p>\n\n\n\n<p>type: print('Hello World!')<\/p>\n\n\n\n<p>press ENTER<\/p>\n\n\n\n<p>The effect should be like&nbsp;this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1019\" height=\"336\" src=\"http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Hello-world.png\" alt class=\"wp-image-87\" srcset=\"http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Hello-world.png 1019w, http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Hello-world-300x99.png 300w, http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Hello-world-768x253.png 768w\" sizes=\"(max-width: 1019px) 100vw, 1019px\"><\/figure>\n\n\n\n<p>We told Python to type out (print) the phrase Hello World! \u2014 and Python did&nbsp;it.<\/p>\n\n\n\n<p>If you've achieved the above effect, congratulations! You just wrote your first Python program.<br>Let us note again how the shell works. The typed commands are executed immediately. That is why it is suitable for short, ad hoc exercises \u2014 and basically only for&nbsp;that.<\/p>\n\n\n<h2 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Writing and reading<\/h2>\n\n\n<p>Writing and reading are two basic skills in today's world, right? The third is definitely the ability to count. Likewise, with coding, \"writing\" and \"reading\" are the two most important ways you communicate with the computer. When you want the computer to \"expose information\", you will use&nbsp;the <em>write<\/em> command. On the other hand, when the computer is supposed to \"receive information\", we will use the <em>read<\/em> command. As it has been for many many years in the good old PASCAL\u2026 Unfortunately, in Python, there is no <em>write<\/em> command and&nbsp;no <em>read<\/em> command. Instead, we have <em>print<\/em> and <em>input<\/em><a href=\"#_ftn1\"><em><strong>[1]<\/strong><\/em><\/a>. We&nbsp;have already learned how print works. So it's time for&nbsp;input.<\/p>\n\n\n\n<p>Remember about the parentheses for Python commands. The correct syntax looks like&nbsp;this:<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">input()<\/p>\n\n\n<p>When you write the input () command, you assume that someone \u2014 some user \u2014 will open the script you wrote and that script will ask them for some information. Thanks to the input () command contained in the code, the information given by the user will be \"read\" by the program.<\/p>\n\n\n\n<p>For example, it might look like&nbsp;this:<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">print('Enter your&nbsp;name:')<\/p>\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">input()<\/p>\n\n\n<p>As you can see, here we have a program consisting of two lines of code. The first line is the command to write a message to the user. The second line is the command to read the answer given by the&nbsp;user.<\/p>\n\n\n\n<p>Considering the user's involvement in the current task, it is difficult to imagine further work in the Python shell. Recall: the shell immediately executes the commands. Therefore, it is not suitable for activities in which you, as the author, create a program, and later someone else (the user) runs it on his computer. \"Later\" in this case means \"whenever he wants\". Whether it's in a minute, an hour or a year. Therefore, in order to make your script permanent, we will save it to a file. The user will need to run the file first to execute the commands.<\/p>\n\n\n\n<p>When you want to save the code as a file, in IDLE: select File menu, command New File. In the newly opened window, enter the two mentioned lines of&nbsp;code:<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">print('Enter your name: ')<br>input()<\/p>\n\n\n<p>Now select \"Save as \u2026\" from the File menu, give the file a name such as Exercise01 and save it with the extension .py in the directory where you will store your training files.<\/p>\n\n\n\n<p>Great, you just created your first Python script which you saved as a file. We will check how our script works in a moment. A little more patience.<\/p>\n\n\n<h2 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Working with Python files<\/h2>\n\n\n<p>Working with Python files is very easy. We have just shown you how to save new content to a&nbsp;file.<\/p>\n\n\n<h3 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Create files<\/h3>\n\n\n<p>Developers work with files in two ways. When their creative inspiration comes and they want to write a piece of code, they use menu in IDLE. They open a new file (File \u2192 New File) and enter the code commands they invented, and then save it. Others, who do not like IDLE, use a simple text editor (for example Microsoft Notepad, which is a regular notebook probably known to every Windows user). We mentioned it in the first post and promised to get back to the&nbsp;topic.<\/p>\n\n\n\n<p>Yes it's true \u2014 you can save your code in a notepad file. When you write a few lines of code in your notebook and decide that you want to save the effect of your work, select the \"Save As \u2026\" command, give the file a name, but instead of the suggested .txt file extension, type py after the dot. This way you have a Python script ready to run at any&nbsp;time.<\/p>\n\n\n<h3 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Running existing files<\/h3>\n\n\n<p>How is it with running files? The easiest way to run a Python file is to find it in the correct directory (for example using the well-known File Explorer) and double-click the mouse. Remember that opening a file actually means running it: the Python interpreter will execute the commands you have written to the&nbsp;file.<\/p>\n\n\n\n<p>However, there may be times when you want to open a file in order to edit it further, without running it. Then, after selecting a file from the list, right-click it and select \"Open with\", and then select Notebook from the list of available programs.<\/p>\n\n\n\n<p>You can also run the file with IDLE. Select the file, right click it and select \"Edit with IDLE\". The file will be opened for editing. Now select the <em>Run menu<\/em>, <em>Run module<\/em> command. The file will&nbsp;run.<\/p>\n\n\n\n<p>In the early stages of learning Python, it's a good idea to run files this way. This has the advantage that the effect of the file (\"end screen\") remains visible in the shell until you close it yourself. On the other hand, when you run the file using the traditional method, the generated content appears and disappears immediately. For inexperienced programmers, this is extremely irritating; they describe the effect as follows: I found the Python file in the directory, chose it with the mouse, clicked twice. A black screen flashed before my eyes and disappeared.<\/p>\n\n\n\n<p>Once we get to know Python a little better, we promise to come back to the topic and present a simple trick to prevent the screen from disappearing. For today, we advise you to run files via IDLE. Considering that we will (for now) write rather short \"training\" scripts, this way of running them will be the most convenient.<\/p>\n\n\n<h3 class=\"fonts-plugin-block \" style=\"font-weight: normal\">We check how our file&nbsp;works<\/h3>\n\n\n<p>It's time to run the script we wrote in Exercise01.py.<\/p>\n\n\n\n<p>Find this file on your computer, select it with a single mouse click, then right click and select \"Edit with IDLE\". Our file will open as for editing<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"730\" height=\"187\" src=\"http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Print-and-input.png\" alt class=\"wp-image-88\" srcset=\"http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Print-and-input.png 730w, http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Print-and-input-300x77.png 300w\" sizes=\"(max-width: 730px) 100vw, 730px\"><\/figure>\n\n\n\n<p>Now select Run \u2192 Run Module<\/p>\n\n\n\n<p>The file is launched. The first command is carried out, which is the command to display the sentence \"Enter your name\" on the screen.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"320\" src=\"http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Enter-your-name.png\" alt class=\"wp-image-89\" srcset=\"http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Enter-your-name.png 1024w, http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Enter-your-name-300x94.png 300w, http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Enter-your-name-768x240.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\"><\/figure>\n\n\n\n<p>Type in any name and press Enter. Nothing special is going to happen because \u2014 so far \u2014 we haven't told Python how to react to a user entering a name. However, you can be sure that Python will remember the name you entered. It's always been a little success, isn't&nbsp;it?<\/p>\n\n\n\n<p>In the next post, we will expand our script and try to have a slightly longer conversation between our program and the&nbsp;user.<\/p>\n\n\n<h2 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Summary<\/h2>\n\n\n<p>In this passage, we wrote our first Python script saved as a file. The file can be run by the user at any time. The script contained in this file will exchange information with the user, writing messages to the user (print command) and reading the responses given by the user (input command).<\/p>\n\n\n\n<p>From now on, we will write and save our code in Python files, and the shell will only play an auxiliary role.<\/p>\n\n\n<h2 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Appendix<\/h2>\n\n\n<p>In this appendix, we'll try to take a deeper look at the input() command. So we will have to use a bit more technical (specialized) vocabulary.<\/p>\n\n\n\n<p>What we commonly call a command is in fact a Python function. There will be time to learn more about the functions later. For now, let's just remember that input() is one of Python's built-in (that is, ready-to-use) functions. When the input() function is called, the screen displays a cursor waiting for a string to be entered. In other words: when the input() function is executed, the program flow stops until the user enters the specified value.<\/p>\n\n\n\n<p>Another important observation: each programming language has strictly defined <em>data types<\/em> that a program written in that language can accept. Python is no different. Examples of data types in Python are:<\/p>\n\n\n\n<ul><li>strings \u2014 that is a string of characters; simple text type; it's just a sequence of Unicode characters,<\/li><li>numeric types (numeric) \u2014 there are three of them, incl. int type (short for integer), representing integers,<\/li><li>logical type (boolean) \u2014 True \/&nbsp;False.<\/li><\/ul>\n\n\n\n<p>Data that the user enters \"from the keyboard\" cannot be sent directly to the program, as they must first be given a type acceptable to Python. Regarding external input, the developers of Python decided that Python would accept data in the string format only.<\/p>\n\n\n\n<p>Therefore, the input() function does three things:<\/p>\n\n\n\n<ul><li>reads data entered from the keyboard,<\/li><li>converts them into a string,<\/li><li>returns to the program.<\/li><\/ul>\n\n\n\n<p>What does this mean in practice?<\/p>\n\n\n\n<p>When you enter two characters, such as <strong>12<\/strong>, from the keyboard, Python reads them as a string, not a number. Characters are just characters, symbols, they have no numerical value for Python. To find out, do a simple Python shell exercise, write two strings connected with a plus and hit ENTER, e.g. '12' + '3'<\/p>\n\n\n\n<p>By the way, it is worth mentioning here that <em>returning value<\/em> is one of the most explored topics in various Python guides and tutorials. Undoubtedly, we will encounter it more than&nbsp;once.<\/p>\n\n\n\n<p>Probably reading that the input() function returns a string value to the program, you ask yourself: wait a minute, this is not always the desired situation. What if we want the user to give us a number? What then? When we ask the user for their first name, the text-based answer is absolutely OK. However, when asked (for example) for his age, the answer given should be recognized as a number. Python solves this easily. It is recommended to use the int() function, which converts a string into a number. To achieve the desired effect, wrap the function input() with the function int(), that is,&nbsp;write:<\/p>\n\n\n\n<p>int(input())<\/p>\n\n\n\n<hr class=\"wp-block-separator\">\n\n\n\n<p><a href=\"#_ftnref1\">[1]<\/a> in fact, read and write exist in Python, but for the purpose of working with database files<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p id=\"Odcinek-2\">Odcinek\u20112<\/p>\n\n\n<h1 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Odcinek 2:&nbsp; Hello World!<\/h1>\n\n\n<p>Witajcie w drugim odcinku naszego cyklu po\u015bwi\u0119conego j\u0119zykowi programowania Python.<\/p>\n\n\n\n<p>Mamy ju\u017c Pythona oraz IDLE zainstalowane na naszym komputerze. Mo\u017cemy dzia\u0142a\u0107!<\/p>\n\n\n\n<p>Je\u015bli nie pami\u0119tasz, czym jest IDLE, zerknij do poprzedniego postu.<\/p>\n\n\n\n<p>Pierwsze kroki wykonamy w oknie g\u0142\u00f3wnym IDLE, kt\u00f3re nosi nazw\u0119 \"pow\u0142oki\" (shell).<\/p>\n\n\n\n<p>Pow\u0142oka to interaktywne okno, \u015bwietnie nadaj\u0105ce si\u0119 do pisania bardzo kr\u00f3tkich program\u00f3w (skrypt\u00f3w), przeznaczonych do cel\u00f3w czysto treningowych. I w\u0142a\u015bnie od pisania-mini skrypt\u00f3w w shellu zacznie\u00admy. W przysz\u0142o\u015bci, gdy zechcemy pisa\u0107 odrobin\u0119 bardziej rozbudowane skrypty, b\u0119dziemy to robi\u0107 w&nbsp;<strong>plikach<\/strong> Pythona.<\/p>\n\n\n\n<p>Jest taki przes\u0105d w kr\u0119gach informatyk\u00f3w, i\u017c nie poszcz\u0119\u015bci Ci si\u0119 w programowaniu, je\u015bli nie napi\u00adszesz swojego pierwszego programu, polecaj\u0105c wygenerowanie na ekranie napisu &nbsp;Hello World! (Witaj \u015awiecie!).<\/p>\n\n\n\n<p>Skoro tak, zr\u00f3bmy to! Tekst, kt\u00f3ry Python ma dla Ciebie \"wydrukowa\u0107\" (wy\u015bwietli\u0107 na ekranie), nazywa si\u0119 fachowo <em>\u0142a\u0144cuchem znak\u00f3w<\/em>. Pocz\u0105tek i koniec \u0142a\u0144cucha znak\u00f3w musimy (obowi\u0105zkowo) oznaczy\u0107 cudzys\u0142owem (lub je\u015bli wolisz: apostrofem):<\/p>\n\n\n\n<p>\"Hello World!\"&nbsp;&nbsp; lub&nbsp; 'Hello World!'<\/p>\n\n\n\n<p>Pora pozna\u0107 pierwsze polecenie ze \"sk\u0142adni\" (syntaksu) Pythona<a href=\"#_ftn1\">[1]<\/a>. Tym pierwszym poleceniem, jakie poznamy, jest print(). Zwr\u00f3\u0107 uwag\u0119 na nawiasy, u\u017cyte po s\u0142owie print. W nich umieszczasz tre\u015b\u0107, jak\u0105 chcesz wydrukowa\u0107.<\/p>\n\n\n\n<p>Zatem:<\/p>\n\n\n\n<p>otw\u00f3rz IDLE<\/p>\n\n\n\n<p>wpisz:&nbsp;&nbsp; print('Hello World!')<\/p>\n\n\n\n<p>naci\u015bnij&nbsp; Enter<\/p>\n\n\n\n<p>Efekt powinien by\u0107&nbsp;taki:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1019\" height=\"336\" src=\"http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Hello-world.png\" alt class=\"wp-image-87\" srcset=\"http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Hello-world.png 1019w, http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Hello-world-300x99.png 300w, http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Hello-world-768x253.png 768w\" sizes=\"(max-width: 1019px) 100vw, 1019px\"><\/figure>\n\n\n\n<p>Nakazali\u015bmy Pythonowi wypisa\u0107 (wydrukowa\u0107) fraz\u0119&nbsp; Hello World! \u2013 i Python to wykona\u0142.<\/p>\n\n\n\n<p>Je\u015bli uzyska\u0142e\u015b powy\u017cszy efekt, gratulacje! W\u0142a\u015bnie napisa\u0142e\u015b sw\u00f3j pierwszy program w Pythonie.<\/p>\n\n\n\n<p>Zauwa\u017cmy raz jeszcze, jak dzia\u0142a shell. Wpisywane polecenia s\u0105 natychmiast wykonywane. Dlatego&nbsp;w\u0142a\u015bnie nadaje si\u0119 on do kr\u00f3tkich, dora\u017anych \u0107wicze\u0144 \u2013 &nbsp;i w zasadzie tylko do&nbsp;tego.<\/p>\n\n\n<h2 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Pisanie i czytanie<\/h2>\n\n\n<p>Pisanie i czytanie to dwie podstawowe umiej\u0119tno\u015bci w dzisiejszym \u015bwiecie, prawda? Trzeci\u0105 jest na&nbsp;pewno umiej\u0119tno\u015b\u0107 liczenia.<\/p>\n\n\n\n<p>Podobnie b\u0119dzie w kodowaniu: \"pisanie\" i \"czytanie\" to dwa najwa\u017cniejsze sposoby twojego komunikowania si\u0119 z komputerem. Gdy chcesz, aby komputer \"da\u0142 informacj\u0119\", u\u017cyjesz polecenia <em>napisz<\/em>. Gdy natomiast komputer ma \"przyj\u0105\u0107 informacj\u0119\", u\u017cyjemy polecenia <em>przeczytaj<\/em>.<\/p>\n\n\n\n<p>W Pythonie niestety polecenie <em>napisz<\/em> nie brzmi \"write\", a polecenie <em>przeczytaj<\/em> nie brzmi \"read\". Tak&nbsp;jak to by\u0142o dawno temu cho\u0107by w poczciwym PASCALu. Zamiast tego mamy <em>print<\/em> i <em>input<\/em>.<\/p>\n\n\n\n<p>Poznali\u015bmy ju\u017c jak dzia\u0142a print. Czas wi\u0119c na&nbsp;input.<\/p>\n\n\n\n<p>Pami\u0119taj o nawiasach przy poleceniach Pythona. Prawid\u0142owa sk\u0142adnia wygl\u0105da tak:<\/p>\n\n\n\n<p>input()<\/p>\n\n\n\n<p>Pisz\u0105c polecenie input() zak\u0142adasz, \u017ce kto\u015b \u2013 jaki\u015b u\u017cytkownik \u2013 otworzy napisany przez Ciebie skrypt i ten skrypt poprosi go o podanie pewnej informacji. Dzi\u0119ki zawartemu w kodzie poleceniu input(), podana przez u\u017cytkownika informacja zostanie \"zaczytana\" przez program.<\/p>\n\n\n\n<p>Mo\u017ce to wygl\u0105da\u0107 na przyk\u0142ad tak:<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">print('Podaj swoje imi\u0119:')<br>input()<\/p>\n\n\n<p>Jak wida\u0107, mamy tu program sk\u0142adaj\u0105cy si\u0119 z dw\u00f3ch linijek kodu. Pierwsza linijka stanowi polecenie napisania komunikatu dla u\u017cytkownika. Druga linijka stanowi polecenie zaczytania odpowiedzi udzielonej przez u\u017cytkownika.<\/p>\n\n\n\n<p>Bior\u0105c pod uwag\u0119 udzia\u0142 u\u017cytkownika w obecnym zadaniu, trudno wyobrazi\u0107 sobie dalsz\u0105 prac\u0119 w pow\u0142oce Pythona (shellu). Przypomnijmy: shell natychmiast wykonuje zadane polecenia. Nie nadaje si\u0119 zatem do takich dzia\u0142a\u0144, w kt\u00f3rych Ty jako autor tworzysz program, a p\u00f3\u017aniej kto\u015b inny (u\u017cyt\u00adkownik) uruchamia go na swoim komputerze. \"P\u00f3\u017aniej\" oznacza w tym przypadku: \"kiedykolwiek zechce\". Czy&nbsp;to za minut\u0119, za godzin\u0119, czy za&nbsp;rok.<\/p>\n\n\n\n<p>Dlatego, w celu utrwalenia twojego skryptu, zapiszemy go w pliku. U\u017cytkownik, aby wykona\u0107 polecenia, b\u0119dzie musia\u0142 najpierw uruchomi\u0107 plik.<\/p>\n\n\n\n<p>Gdy chcesz zapisa\u0107 kod w postaci pliku, wybierz w IDLu: &nbsp;<em>menu File,<\/em> polecenie &nbsp;<em>New File<\/em>. W&nbsp;nowo-otwartym oknie wpisz dwie wspomniane linie&nbsp;kodu:<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">print('Podaj swoje imi\u0119:')<br>input()<\/p>\n\n\n<p>Teraz z menu File wybierz polecenie \"Save as\u2026\", nadaj plikowi jak\u0105\u015b nazw\u0119, np. Exercise01 i zapisz go z rozszerzeniem&nbsp; .py w katalogu, w kt\u00f3rym przechowywa\u0142 b\u0119dziesz swoje pliki treningowe.<\/p>\n\n\n\n<p>\u015awietnie, w\u0142a\u015bnie stworzy\u0142e\u015b sw\u00f3j pierwszy skrypt w Pythonie, kt\u00f3ry zapisa\u0142e\u015b w formie pliku.<\/p>\n\n\n\n<p>Za chwilk\u0119 sprawdzimy jak nasz skrypt dzia\u0142a. Jeszcze troch\u0119 cierpliwo\u015bci.<\/p>\n\n\n<h2 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Praca z plikami Pythona<\/h2>\n\n\n<p>Praca z plikami Pythona jest banalnie prosta. Przed chwil\u0105 pokazali\u015bmy, jak zapisa\u0107 now\u0105 tre\u015b\u0107 w&nbsp;pliku.<\/p>\n\n\n<h3 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Tworzenie plik\u00f3w<\/h3>\n\n\n<p>Programi\u015bci pracuj\u0105 z plikami na dwa sposoby. Gdy najdzie ich tw\u00f3rcza wena i chc\u0105 zapisa\u0107 jaki\u015b fragment kodu, korzystaj\u0105 z menu w IDLu. Otwieraj\u0105 nowy plik (File \u2192 New File) i wprowadzaj\u0105 wymy\u015blone przez siebie polecenia kodu, a nast\u0119pnie zapisuj\u0105.<\/p>\n\n\n\n<p>Inni, kt\u00f3rzy nie przepadaj\u0105 za IDLem, korzystaj\u0105 z prostego edytora tekstu (przyk\u0142adowo Microsoft Notepada, czyli zwyk\u0142ego notatnika znanego pewnie ka\u017cdemu u\u017cytkownikowi Windowsa). Wspomnieli\u015bmy o nim w pierwszym po\u015bcie i obiecali\u015bmy wr\u00f3ci\u0107 do tematu.<\/p>\n\n\n\n<p>Tak, to prawda. Tw\u00f3j kod mo\u017cesz zapisywa\u0107 w pliku notatnika. Gdy napiszesz w notatniku kilka linijek kodu i&nbsp;stwierdzisz, \u017ce chcesz zachowa\u0107 efekt pracy, wybierz polecenie \"Zapisz jako\u2026\", nadaj&nbsp;plikowi nazw\u0119, ale zamiast sugerowanego rozszerzenia nazwy pliku .txt wpisz po kropce py. W ten spos\u00f3b masz gotowy skrypt Pythona, do uruchomienia w ka\u017cdej chwili.<\/p>\n\n\n<h3 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Uruchamianie istniej\u0105cych plik\u00f3w<\/h3>\n\n\n<p>No w\u0142a\u015bnie. A jak to jest z uruchamianiem plik\u00f3w?<\/p>\n\n\n\n<p>Najprostszy spos\u00f3b uruchamiania pliku Pythona to odnalezienie go we w\u0142a\u015bciwym katalogu (cho\u0107by za pomoc\u0105 dobrze Ci znanego Eksploratora Plik\u00f3w) i dwukrotne klikni\u0119cie mysz\u0105. Pami\u0119taj, \u017ce otwarcie pliku oznacza de facto jego uruchomienie: interpreter Pythona wykona polecenia, kt\u00f3re zapisa\u0142e\u015b w&nbsp;pliku.<\/p>\n\n\n\n<p>Mo\u017ce jednak si\u0119 zdarzy\u0107, \u017ce chcesz otworzy\u0107 plik w celu dalszego jego edytowania, bez uruchamiania. W\u00f3wczas po wybraniu pliku z listy kliknij prawym przyciskiem myszy i wybierz opcj\u0119 \"Otw\u00f3rz za pomoc\u0105\", a nast\u0119pnie z listy dost\u0119pnych program\u00f3w wybierz notatnik.<\/p>\n\n\n\n<p>Mo\u017cesz tak\u017ce uruchomi\u0107 plik przy pomocy IDLa. Wybierz plik, kliknij go prawym przyciskiem i&nbsp;wybierz \"Edit with IDLE\". Plik zostanie otwarty do edycji. Teraz wybierz menu <strong>Run<\/strong>, polecenie <strong>Run&nbsp;module<\/strong>. Plik zostanie uruchomiony.<\/p>\n\n\n\n<p>W pocz\u0105tkowej fazie nauki Pythona warto w\u0142a\u015bnie uruchamia\u0107 pliki tym sposobem. Ma to t\u0119 zalet\u0119, \u017ce&nbsp;efekt dzia\u0142ania pliku (\"ekran ko\u0144cowy\") pozostaje widoczny w shellu tak d\u0142ugo, dop\u00f3ki sam go nie&nbsp;zamkniesz. Gdy natomiast uruchamiasz plik tradycyjn\u0105 metod\u0105, generowana tre\u015b\u0107 pojawia si\u0119 i&nbsp;natychmiast znika. Dla niedo\u015bwiadczonych programist\u00f3w jest to wyj\u0105tkowo irytuj\u0105ce; opisuj\u0105 oni \u00f3w&nbsp;efekt nast\u0119puj\u0105co: odnalaz\u0142em interesuj\u0105cy mnie plik Pythona w katalogu, najecha\u0142em mysz\u0105, dwa&nbsp;razy klikn\u0105\u0142em. Mrygn\u0119\u0142o mi co\u015b przed oczyma, jakby czarny ekran i znik\u0142o.<\/p>\n\n\n\n<p>Gdy troch\u0119 lepiej poznamy Pythona, obiecujemy wr\u00f3ci\u0107 do tematu i zaprezentowa\u0107 prosty trick, zapobiegaj\u0105cy \"znikaniu ekranu\". Na dzi\u015b radzimy uruchamia\u0107 pliki poprzez IDLa. Bior\u0105c pod uwag\u0119, \u017ce (p\u00f3ki co) pisa\u0107 b\u0119dziemy raczej kr\u00f3tkie skrypty \"treningowe\", ten spos\u00f3b ich uruchamiania b\u0119dzie najbardziej wygodny.<\/p>\n\n\n<h3 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Sprawdzamy jak dzia\u0142a nasz&nbsp;plik<\/h3>\n\n\n<p>Czas uruchomi\u0107 napisany przez nas skrypt, zapisany w pliku Exercise01.py.<\/p>\n\n\n\n<p>Znajd\u017a ten plik na swoim komputerze, wybierz go pojedynczym klikni\u0119ciem myszy, nast\u0119pnie kliknij prawym przyciskiem i wybierz \"Edit with IDLE\". Nasz plik zostanie otwarty jak do edycji<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"730\" height=\"187\" src=\"http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Print-and-input.png\" alt class=\"wp-image-88\" srcset=\"http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Print-and-input.png 730w, http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Print-and-input-300x77.png 300w\" sizes=\"(max-width: 730px) 100vw, 730px\"><\/figure>\n\n\n\n<p>Teraz wybierz Run \u2192 Run Module<\/p>\n\n\n\n<p>Plik zostaje uruchomiony. Wykonane zostaje pierwsze polecenie, czyli polecenie wy\u015bwietlenia na ekranie zdania: \"Podaj swoje&nbsp;imi\u0119\".<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"320\" src=\"http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Enter-your-name.png\" alt class=\"wp-image-89\" srcset=\"http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Enter-your-name.png 1024w, http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Enter-your-name-300x94.png 300w, http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Enter-your-name-768x240.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\"><\/figure>\n\n\n\n<p>Wpisz dowolne imi\u0119 i naci\u015bnij Enter. Nic specjalnego si\u0119 nie wydarzy, poniewa\u017c \u2013 p\u00f3ki co \u2013 nie wskazali\u015bmy Pythonowi, jak ma zareagowa\u0107 na wpisanie imienia przez u\u017cytkownika. Mo\u017cesz jednak by\u0107 pewnym, \u017ce Python zapami\u0119ta\u0142 wpisane przez Ciebie imi\u0119<a href=\"#_ftn2\">[2]<\/a>. To ju\u017c zawsze jaki\u015b ma\u0142y sukces, prawda?<\/p>\n\n\n\n<p>W kolejnym po\u015bcie rozbudujemy nasz skrypt i pokusimy si\u0119 o troszeczk\u0119 d\u0142u\u017csz\u0105 konwersacj\u0119 naszego programu z u\u017cytkownikiem.<\/p>\n\n\n<h2 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Podsumowanie<\/h2>\n\n\n<p>W tym odcinku napisali\u015bmy nasz pierwszy skrypt zapisany w pliku Pythona. Plik mo\u017ce zosta\u0107 w dowolnym czasie uruchomiony przez u\u017cytkownika. Skrypt, zawarty w tym pliku prowadzi\u0142 b\u0119dzie wymian\u0119 informacji z u\u017cytkownikiem, pisz\u0105c do u\u017cytkownika komunikaty (polecenie print) i odczytuj\u0105c udzielane przez u\u017cytkownika odpowiedzi (polecenie input).<\/p>\n\n\n\n<p>Od tej pory b\u0119dziemy pisa\u0107 i zachowywa\u0107 nasz kod w\u0142a\u015bnie w plikach Pythona, za\u015b shell (pow\u0142oka) pe\u0142ni\u0107 b\u0119dzie jedynie pomocnicz\u0105 rol\u0119.<\/p>\n\n\n<h2 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Dodatek<\/h2>\n\n\n<p>W tym dodatku spr\u00f3bujemy nieco g\u0142\u0119biej spojrze\u0107 na polecenie input(). A zatem b\u0119dziemy musieli pos\u0142u\u017cy\u0107 si\u0119 nieco bardziej technicznym (specjalistycznym) s\u0142ownictwem.<\/p>\n\n\n\n<p>To co w potocznym j\u0119zyku nazywamy poleceniem, <em>de facto<\/em> jest funkcj\u0105 w Pythonie.<\/p>\n\n\n\n<p>Na dok\u0142adniejsze poznanie funkcji przyjdzie czas p\u00f3\u017aniej. Na razie zapami\u0119tajmy tylko tyle, \u017ce input() jest jedn\u0105 z wbudowanych (to znaczy gotowych do u\u017cycia) funkcji Pythona.<\/p>\n\n\n\n<p>Po wywo\u0142aniu funkcji input() na ekranie ukazuje si\u0119 kursor oczeki\u00adwania na wprowadzenie ci\u0105gu znak\u00f3w. Innymi s\u0142owy: gdy funkcja input() jest wykonywana, przep\u0142yw programu zatrzymuje si\u0119, dop\u00f3ki u\u017cytkownik nie wprowadzi okre\u015blonej warto\u015bci.<\/p>\n\n\n\n<p>Kolejne wa\u017cne spostrze\u017cenie: ka\u017cdy j\u0119zyk programowania ma \u015bci\u015ble zdefiniowane typy danych, jakie program napisany w tym j\u0119zyku mo\u017ce zaakceptowa\u0107. Nie inaczej jest w Pythonie. Przyk\u0142adowe typy danych w Pythonie to:<\/p>\n\n\n\n<ul><li>strings \u2013 czyli ci\u0105g (\u0142a\u0144cuch) znak\u00f3w; prosty typ tekstowy; jest to po prostu sekwencja znak\u00f3w Unicode,<\/li><li>typy liczbowe (numeric) \u2013 jest ich trzy, w tym m.in. typ int (skr\u00f3t od s\u0142owa integer), reprezentuj\u0105cy liczby ca\u0142kowite,<\/li><li>typ logiczny (boolean) \u2013 Prawda \/&nbsp;Fa\u0142sz.<\/li><\/ul>\n\n\n\n<p>Dane, kt\u00f3re u\u017cytkownik wprowadza \"z klawiatury\" nie mog\u0105 trafi\u0107 bezpo\u015brednio do programu, gdy\u017c najpierw musi by\u0107 im nadany typ akceptowalny przez Pythona.<\/p>\n\n\n\n<p>Odno\u015bnie danych wprowadzanych z zewn\u0105trz, tw\u00f3rcy Pythona zadecydowali, \u017ce j\u0119zyk ten akceptowa\u0142 b\u0119dzie dane w formacie strings (format \u0142a\u0144cucha znak\u00f3w).<\/p>\n\n\n\n<p>Z tego wzgl\u0119du funkcja input() wykonuje trzy czynno\u015bci:<\/p>\n\n\n\n<ul><li>czyta dane wprowadzone z klawiatury,<\/li><li>konwertuje je do postaci stringu (\u0142a\u0144cucha),<\/li><li>zwraca do programu.<\/li><\/ul>\n\n\n\n<p>Co to oznacza w praktyce?<\/p>\n\n\n\n<p>Gdy wprowadzisz z klawiatury dwa znaki, np. <strong>12<\/strong>, Python odczyta je jako \u0142a\u0144cuch znak\u00f3w, a&nbsp;nie&nbsp;liczb\u0119. Znaki to po prostu znaki, symbole, nie maj\u0105 one dla Pythona warto\u015bci w sensie liczbowym. Aby si\u0119 przekona\u0107, wykonaj w shellu Pythona proste \u0107wiczenie, napisz dwa \u0142a\u0144cuchy znak\u00f3w po\u0142\u0105czone plusem i&nbsp;naci\u015bnij Enter, np. '12'+'3'<\/p>\n\n\n\n<p>Warto w tym miejscu napomkn\u0105\u0107, \u017ce zwracanie warto\u015bci przez funkcje (returning value) to jeden z&nbsp;intensywniej eksplorowanych temat\u00f3w w rozmaitych poradnikach i samouczkach dotycz\u0105cych Pythona. Bez w\u0105tpienia jeszcze nie raz si\u0119 z nim zetkniemy.<\/p>\n\n\n\n<p>Zapewne czytaj\u0105c, i\u017c funkcja input() zwraca do programu warto\u015b\u0107 w postaci \u0142a\u0144cucha znak\u00f3w, zadajesz sobie pytanie: no ale chwileczk\u0119, przecie\u017c nie zawsze jest to sytuacja po\u017c\u0105dana. A je\u015bli chcemy aby u\u017cytkownik poda\u0142 nam liczb\u0119? Co w\u00f3wczas?<\/p>\n\n\n\n<p>Gdy pytamy u\u017cytkownika o imi\u0119, odpowied\u017a w formie tekstu jest jak najbardziej OK. Jednak gdy zapytamy go (przyk\u0142adowo) o wiek, udzielona odpowied\u017a powinna by\u0107 rozpoznana jako liczba.<\/p>\n\n\n\n<p>Python rozwi\u0105zuje to w prosty spos\u00f3b. Rekomendowane jest pos\u0142u\u017cenie si\u0119 funkcj\u0105 int(), kt\u00f3ra przekszta\u0142ca \u0142a\u0144cuch znak\u00f3w w liczb\u0119. Aby osi\u0105gn\u0105\u0107 zamierzony efekt, opakuj funkcj\u0119 input() funkcj\u0105 int(), czyli napisz:<\/p>\n\n\n\n<p>int(input())<\/p>\n\n\n\n<hr class=\"wp-block-separator\">\n\n\n\n<p><a href=\"#_ftnref1\">[1]<\/a> Przy okazji: warto oswoi\u0107 si\u0119 ze s\u0142owem \"syntaks\", gdy\u017c ucz\u0105c si\u0119 Pythona us\u0142yszysz je zapewne jeszcze z milion razy.<\/p>\n\n\n\n<p><a href=\"#_ftnref2\">[2]<\/a> po uruchomieniu skryptu, dane wprowadzane przez u\u017cytkownika zapami\u0119tywane s\u0105 kr\u00f3tkotrwale, to znaczy jedynie do momentu zamkni\u0119cia tego skryptu; w p\u00f3\u017aniejszej fazie nauki poznamy sposoby trwa\u0142ego zapami\u0119tywania danych w Pythonie.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to the second passage of our Python programming language series. We already have Python&nbsp;[\u2026]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"wp_typography_post_enhancements_disabled":false},"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/juthon.com\/index.php\/wp-json\/wp\/v2\/posts\/85"}],"collection":[{"href":"http:\/\/juthon.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juthon.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juthon.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juthon.com\/index.php\/wp-json\/wp\/v2\/comments?post=85"}],"version-history":[{"count":7,"href":"http:\/\/juthon.com\/index.php\/wp-json\/wp\/v2\/posts\/85\/revisions"}],"predecessor-version":[{"id":114,"href":"http:\/\/juthon.com\/index.php\/wp-json\/wp\/v2\/posts\/85\/revisions\/114"}],"wp:attachment":[{"href":"http:\/\/juthon.com\/index.php\/wp-json\/wp\/v2\/media?parent=85"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juthon.com\/index.php\/wp-json\/wp\/v2\/categories?post=85"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juthon.com\/index.php\/wp-json\/wp\/v2\/tags?post=85"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}