{"id":95,"date":"2021-05-15T12:41:43","date_gmt":"2021-05-15T12:41:43","guid":{"rendered":"http:\/\/juthon.com\/?p=95"},"modified":"2021-10-31T12:50:47","modified_gmt":"2021-10-31T12:50:47","slug":"passage-3-a-few-words-about-variables","status":"publish","type":"post","link":"http:\/\/juthon.com\/index.php\/2021\/05\/15\/passage-3-a-few-words-about-variables\/","title":{"rendered":"Passage 3: A few words about Variables"},"content":{"rendered":"\n<p>Hello again.<\/p>\n\n\n\n<p>In this passage, we'll try to write a simple Python script together. We want the script to: 1 \/ ask the user to enter their first name, 2 \/ read the name the user will provide, 3 \/ write a greeting on the screen: Have a nice day, [name].<\/p>\n\n\n\n<p><a href=\"#Odcinek-3\" data-type=\"internal\" data-id=\"#Odcinek-3\">Jump to Polish version<\/a><\/p>\n\n\n\n<p>The dialogue between the program and the user will look like&nbsp;this:<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">Program: Give your name please.<br>User: Adam<br>Program: Have a nice day,&nbsp;Adam.<\/p>\n\n\n<p>We will focus on the code itself, without any additional technical advice. If you don't know how to save the code to a file and then run it, see the previous post.<\/p>\n\n\n\n<p>As you may have guessed, it will be difficult for the program to use the user's name in the greeting it generates: \"Have a nice day [name]\". In order for the program to use the previously read (remembered) user's name in response, we will have to use a <em>variable<\/em>. Variables are a very important element of any programming language. It's hard to imagine coding without understanding what variables are and how they&nbsp;work.<\/p>\n\n\n\n<p>Okay. Let's start (as a warm-up) with a simplified version of the script to achieve the following effect when launched:<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">Program: Give your name please.<br>User: [name]<br>Program: Have a nice&nbsp;day.<\/p>\n\n\n<p>So for now, without catching the given name by the program and inserting it in the third line (i.e.&nbsp;without using the variable yet).<\/p>\n\n\n\n<p>We can easily write such a program. Here is the&nbsp;code:<\/p>\n\n\n\n<p>Exercise02.py<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">print('Enter your name, please:')<br>input()<br>print('Have a nice&nbsp;day.')<\/p>\n\n\n<p>The program asks the user for his name (print command), remembers it (input command), and finally generates a greeting: Have a nice&nbsp;day.<\/p>\n\n\n\n<p>But how to make the program add the name given by the user after the words Have a nice day?<br>As we already mentioned, we will need a variable for this. Ok, and what exactly is&nbsp;it?<\/p>\n\n\n<h2 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Python Variables<\/h2>\n\n\n<p>In Python, a variable is not the same as in math. A variable in Python is just a label that you tag a piece of data with. Imagine a \"solid\" piece of data, e.g. a list of the largest cities in Germany, that you entered at the very beginning of your new Python script:<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal;font-size: 14px\">&nbsp;&nbsp; ['Aachen', 'Augsburg', 'Bergisch Gladbach', 'Berlin', 'Bielefeld',<br>&nbsp;&nbsp;&nbsp; 'Bochum', 'Bonn', 'Bottrop', 'Braunschweig', 'Bremen',<br>&nbsp;&nbsp;&nbsp; 'Bremerhaven', 'Chemnitz', 'Cologne (K\u00f6ln)',<br>&nbsp;&nbsp;&nbsp; 'Darmstadt', 'Dortmund', 'Dresden', 'Duisburg', 'D\u00fcsseldorf',<br>&nbsp;&nbsp;&nbsp; 'Erfurt', 'Erlangen', 'Essen', 'Frankfurt am Main', 'Freiburg im Breisgau',<br>&nbsp;&nbsp;&nbsp; 'F\u00fcrth', 'Gelsenkirchen', 'G\u00f6ttingen', 'Hagen', 'Halle (Saale)',<br>&nbsp;&nbsp;&nbsp; 'Hamburg', 'Hamm', 'Hanover (Hannover)', 'Heidelberg', 'Heilbronn',<br>&nbsp;&nbsp;&nbsp; 'Herne', 'Hildesheim', 'Ingolstadt', 'Jena', 'Karlsruhe',<br>&nbsp;&nbsp;&nbsp; 'Kassel', 'Kiel', 'Koblenz', 'Krefeld', 'Leipzig',<br>&nbsp;&nbsp;&nbsp; 'Leverkusen', 'L\u00fcbeck', 'Ludwigshafen am Rhein', 'Magdeburg', 'Mainz',<br>&nbsp;&nbsp;&nbsp; 'Mannheim', 'Moers', 'M\u00f6nchengladbach', 'M\u00fclheim an der Ruhr', 'Munich',<br>&nbsp;&nbsp;&nbsp; 'M\u00fcnster', 'Neuss', 'Nuremberg (N\u00fcrnberg)', 'Oberhausen', 'Offenbach am Main',<br>&nbsp;&nbsp;&nbsp; 'Oldenburg', 'Osnabr\u00fcck', 'Paderborn', 'Pforzheim', 'Potsdam',<br>&nbsp;&nbsp;&nbsp; 'Recklinghausen', 'Regensburg', 'Remscheid', 'Reutlingen', 'Rostock',<br>&nbsp;&nbsp;&nbsp; 'Saarbr\u00fccken', 'Salzgitter', 'Siegen', 'Solingen', 'Stuttgart',<br>&nbsp;&nbsp;&nbsp; 'Trier', 'Ulm', 'Wiesbaden', 'Wolfsburg', 'Wuppertal', 'W\u00fcrzburg']<br><\/p>\n\n\n<p>In Python, on an \"object\" such as a list, you can execute various commands, print the list on the screen, sort it, delete selected items or add new items, merge with other lists, etc. However, how to refer to the list in the command? How to bring up the list we want? By typing out all of its contents? Every time we want to execute a command? You admit that it would be extremely tiring. Wouldn't it be easier to name this list with a letter, for example L? Then, in any command, we'll use that one short letter instead of having to tediously write out the entire contents of the list. This our magic letter is just a <em>variable<\/em>.<\/p>\n\n\n\n<p>In Python, a variable is a substitute for the object it has been assigned to. We also call it a reference. Assigning a variable to a list (or any other object) is done using a simple equal&nbsp;sign&nbsp;(=)<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal;font-size: 14px\">&nbsp;&nbsp; L = [<br>    'Aachen', 'Augsburg', 'Bergisch Gladbach', 'Berlin', 'Bielefeld',<br>&nbsp;&nbsp;&nbsp; 'Bochum', 'Bonn', 'Bottrop', 'Braunschweig', 'Bremen',<br>&nbsp;&nbsp;&nbsp; 'Bremerhaven', 'Chemnitz', 'Cologne (K\u00f6ln)',<br>&nbsp;&nbsp;&nbsp; 'Darmstadt', 'Dortmund', 'Dresden', 'Duisburg', 'D\u00fcsseldorf',<br>&nbsp;&nbsp;&nbsp; 'Erfurt', 'Erlangen', 'Essen', 'Frankfurt am Main', 'Freiburg im Breisgau',<br>&nbsp;&nbsp;&nbsp; 'F\u00fcrth', 'Gelsenkirchen', 'G\u00f6ttingen', 'Hagen', 'Halle (Saale)',<br>&nbsp;&nbsp;&nbsp; 'Hamburg', 'Hamm', 'Hanover (Hannover)', 'Heidelberg', 'Heilbronn',<br>&nbsp;&nbsp;&nbsp; 'Herne', 'Hildesheim', 'Ingolstadt', 'Jena', 'Karlsruhe',<br>&nbsp;&nbsp;&nbsp; 'Kassel', 'Kiel', 'Koblenz', 'Krefeld', 'Leipzig',<br>&nbsp;&nbsp;&nbsp; 'Leverkusen', 'L\u00fcbeck', 'Ludwigshafen am Rhein', 'Magdeburg', 'Mainz',<br>&nbsp;&nbsp;&nbsp; 'Mannheim', 'Moers', 'M\u00f6nchengladbach', 'M\u00fclheim an der Ruhr', 'Munich',<br>&nbsp;&nbsp;&nbsp; 'M\u00fcnster', 'Neuss', 'Nuremberg (N\u00fcrnberg)', 'Oberhausen', 'Offenbach am Main',<br>&nbsp;&nbsp;&nbsp; 'Oldenburg', 'Osnabr\u00fcck', 'Paderborn', 'Pforzheim', 'Potsdam',<br>&nbsp;&nbsp;&nbsp; 'Recklinghausen', 'Regensburg', 'Remscheid', 'Reutlingen', 'Rostock',<br>&nbsp;&nbsp;&nbsp; 'Saarbr\u00fccken', 'Salzgitter', 'Siegen', 'Solingen', 'Stuttgart',<br>&nbsp;&nbsp;&nbsp; 'Trier', 'Ulm', 'Wiesbaden', 'Wolfsburg', 'Wuppertal', 'W\u00fcrzburg']<br><\/p>\n\n\n<p>Let's take a look at what the script with the command to print the list above will look&nbsp;like:<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal;font-size: 14px\">L = [<br>&nbsp;&nbsp;&nbsp; 'Aachen', 'Augsburg', 'Bergisch Gladbach', 'Berlin', 'Bielefeld',<br>&nbsp;&nbsp;&nbsp; 'Bochum', 'Bonn', 'Bottrop', 'Braunschweig', 'Bremen',<br>&nbsp;&nbsp;&nbsp; 'Bremerhaven', 'Chemnitz', 'Cologne (K\u00f6ln)',<br>&nbsp;&nbsp;&nbsp; 'Darmstadt', 'Dortmund', 'Dresden', 'Duisburg', 'D\u00fcsseldorf',<br>&nbsp;&nbsp;&nbsp; 'Erfurt', 'Erlangen', 'Essen', 'Frankfurt am Main', 'Freiburg im Breisgau',<br>&nbsp;&nbsp;&nbsp; 'F\u00fcrth', 'Gelsenkirchen', 'G\u00f6ttingen', 'Hagen', 'Halle (Saale)',<br>&nbsp;&nbsp;&nbsp; 'Hamburg', 'Hamm', 'Hanover (Hannover)', 'Heidelberg', 'Heilbronn',<br>&nbsp;&nbsp;&nbsp; 'Herne', 'Hildesheim', 'Ingolstadt', 'Jena', 'Karlsruhe',<br>&nbsp;&nbsp;&nbsp; 'Kassel', 'Kiel', 'Koblenz', 'Krefeld', 'Leipzig',<br>&nbsp;&nbsp;&nbsp; 'Leverkusen', 'L\u00fcbeck', 'Ludwigshafen am Rhein', 'Magdeburg', 'Mainz',<br>&nbsp;&nbsp;&nbsp; 'Mannheim', 'Moers', 'M\u00f6nchengladbach', 'M\u00fclheim an der Ruhr', 'Munich',<br>&nbsp;&nbsp;&nbsp; 'M\u00fcnster', 'Neuss', 'Nuremberg (N\u00fcrnberg)', 'Oberhausen', 'Offenbach am Main',<br>&nbsp;&nbsp;&nbsp; 'Oldenburg', 'Osnabr\u00fcck', 'Paderborn', 'Pforzheim', 'Potsdam',<br>&nbsp;&nbsp;&nbsp; 'Recklinghausen', 'Regensburg', 'Remscheid', 'Reutlingen', 'Rostock',<br>&nbsp;&nbsp;&nbsp; 'Saarbr\u00fccken', 'Salzgitter', 'Siegen', 'Solingen', 'Stuttgart',<br>&nbsp;&nbsp;&nbsp; 'Trier', 'Ulm', 'Wiesbaden', 'Wolfsburg', 'Wuppertal', 'W\u00fcrzburg'<br>&nbsp;&nbsp;&nbsp; ]<br><br>print(L)<br><\/p>\n\n\n<p>\/ the result will be the content of the list printed on your computer screen&nbsp;\/<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>As you can see, we used a short single letter variable in the print command to print the entire long multi-item list.<\/p>\n\n\n\n<p>If we hadn't used the variable, our script would look like&nbsp;this:<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal;font-size: 14px\">[<br>&nbsp;&nbsp;&nbsp; 'Aachen', 'Augsburg', 'Bergisch Gladbach', 'Berlin', 'Bielefeld',<br>&nbsp;&nbsp;&nbsp; 'Bochum', 'Bonn', 'Bottrop', 'Braunschweig', 'Bremen',<br>&nbsp;&nbsp;&nbsp; 'Bremerhaven', 'Chemnitz', 'Cologne (K\u00f6ln)',<br>&nbsp;&nbsp;&nbsp; 'Darmstadt', 'Dortmund', 'Dresden', 'Duisburg', 'D\u00fcsseldorf',<br>&nbsp;&nbsp;&nbsp; 'Erfurt', 'Erlangen', 'Essen', 'Frankfurt am Main', 'Freiburg im Breisgau',<br>&nbsp;&nbsp;&nbsp; 'F\u00fcrth', 'Gelsenkirchen', 'G\u00f6ttingen', 'Hagen', 'Halle (Saale)',<br>&nbsp;&nbsp;&nbsp; 'Hamburg', 'Hamm', 'Hanover (Hannover)', 'Heidelberg', 'Heilbronn',<br>&nbsp;&nbsp;&nbsp; 'Herne', 'Hildesheim', 'Ingolstadt', 'Jena', 'Karlsruhe',<br>&nbsp;&nbsp;&nbsp; 'Kassel', 'Kiel', 'Koblenz', 'Krefeld', 'Leipzig',<br>&nbsp;&nbsp;&nbsp; 'Leverkusen', 'L\u00fcbeck', 'Ludwigshafen am Rhein', 'Magdeburg', 'Mainz',<br>&nbsp;&nbsp;&nbsp; 'Mannheim', 'Moers', 'M\u00f6nchengladbach', 'M\u00fclheim an der Ruhr', 'Munich',<br>&nbsp;&nbsp;&nbsp; 'M\u00fcnster', 'Neuss', 'Nuremberg (N\u00fcrnberg)', 'Oberhausen', 'Offenbach am Main',<br>&nbsp;&nbsp;&nbsp; 'Oldenburg', 'Osnabr\u00fcck', 'Paderborn', 'Pforzheim', 'Potsdam',<br>&nbsp;&nbsp;&nbsp; 'Recklinghausen', 'Regensburg', 'Remscheid', 'Reutlingen', 'Rostock',<br>&nbsp;&nbsp;&nbsp; 'Saarbr\u00fccken', 'Salzgitter', 'Siegen', 'Solingen', 'Stuttgart',<br>&nbsp;&nbsp;&nbsp; 'Trier', 'Ulm', 'Wiesbaden', 'Wolfsburg', 'Wuppertal', 'W\u00fcrzburg'<br>&nbsp;&nbsp;&nbsp; ]<br>print([<br>&nbsp;&nbsp;&nbsp; 'Aachen', 'Augsburg', 'Bergisch Gladbach', 'Berlin', 'Bielefeld',<br>&nbsp;&nbsp;&nbsp; 'Bochum', 'Bonn', 'Bottrop', 'Braunschweig', 'Bremen',<br>&nbsp;&nbsp;&nbsp; 'Bremerhaven', 'Chemnitz', 'Cologne (K\u00f6ln)',<br>&nbsp;&nbsp;&nbsp; 'Darmstadt', 'Dortmund', 'Dresden', 'Duisburg', 'D\u00fcsseldorf',<br>&nbsp;&nbsp;&nbsp; 'Erfurt', 'Erlangen', 'Essen', 'Frankfurt am Main', 'Freiburg im Breisgau',<br>&nbsp;&nbsp;&nbsp; 'F\u00fcrth', 'Gelsenkirchen', 'G\u00f6ttingen', 'Hagen', 'Halle (Saale)',<br>&nbsp;&nbsp;&nbsp; 'Hamburg', 'Hamm', 'Hanover (Hannover)', 'Heidelberg', 'Heilbronn',<br>&nbsp;&nbsp;&nbsp; 'Herne', 'Hildesheim', 'Ingolstadt', 'Jena', 'Karlsruhe',<br>&nbsp;&nbsp;&nbsp; 'Kassel', 'Kiel', 'Koblenz', 'Krefeld', 'Leipzig',<br>&nbsp;&nbsp;&nbsp; 'Leverkusen', 'L\u00fcbeck', 'Ludwigshafen am Rhein', 'Magdeburg', 'Mainz',<br>&nbsp;&nbsp; &nbsp;'Mannheim', 'Moers', 'M\u00f6nchengladbach', 'M\u00fclheim an der Ruhr', 'Munich',<br>&nbsp;&nbsp;&nbsp; 'M\u00fcnster', 'Neuss', 'Nuremberg (N\u00fcrnberg)', 'Oberhausen', 'Offenbach am Main',<br>&nbsp;&nbsp;&nbsp; 'Oldenburg', 'Osnabr\u00fcck', 'Paderborn', 'Pforzheim', 'Potsdam',<br>&nbsp;&nbsp;&nbsp; 'Recklinghausen', 'Regensburg', 'Remscheid', 'Reutlingen', 'Rostock',<br>&nbsp;&nbsp;&nbsp; 'Saarbr\u00fccken', 'Salzgitter', 'Siegen', 'Solingen', 'Stuttgart',<br>&nbsp;&nbsp;&nbsp; 'Trier', 'Ulm', 'Wiesbaden', 'Wolfsburg', 'Wuppertal', 'W\u00fcrzburg'<br>&nbsp;&nbsp;&nbsp; ])<br><\/p>\n\n\n<p>\/ the result will be the content of the list printed on your computer screen&nbsp;\/<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Failure to use a variable means (as you can see) the need to rewrite the entire long list in the print command, even though the list above has already been entered in the code&nbsp;once.<\/p>\n\n\n\n<p>It's not hard to imagine what your script would look like if you wanted to enter more commands for this list. The list would be rewritten a great many times and the script would be devastatingly long and tiring to&nbsp;read.<\/p>\n\n\n\n<p>The variable, therefore, saves us from thoughtless and tedious code duplication. But is it&nbsp;only?<\/p>\n\n\n<h2 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Variable as Interceptor<\/h2>\n\n\n<p>The variable gives something more than what we wrote a moment ago. It allows you to refer to elements of the program that cannot be referred to in any other way. We mean the data entered by the user when the program is started \u2014 as in our case \"name\". After all, we cannot in any way predict what name the user will enter when the program asks for it. It may well be the name Adam and Thomas or&nbsp;Sarah.<\/p>\n\n\n\n<p>User input is not a snippet of code. These are data entered \"from the outside\" in the program start-up phase. That is why they are called run-time data, and often also transient data, because when the program is closed, they disappear into oblivion.<\/p>\n\n\n\n<p>To put it in a very simple language: the data entered during the program operation phase must be named somehow in order to be able to refer to it&nbsp;later.<\/p>\n\n\n\n<p>Or to put it a bit more in a professional language: this data has to be assigned to a variable in order to be able to refer to it&nbsp;later.<\/p>\n\n\n\n<p>The variable is assigned in the same way as in the above-mentioned list example. You choose any letter of the alphabet, say \"a\", and assign it to the entered data:<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">a =&nbsp;input()<\/p>\n\n\n<p>We have marked the data that the user enters into the program as a. Now we can easily tell the program to print this&nbsp;data:<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">print (a)<\/p>\n\n\n<p>Simple and easy,&nbsp;right?<\/p>\n\n\n\n<p>We can finally return to our script from the beginning of the article and present its full&nbsp;form:&nbsp;<\/p>\n\n\n\n<p>Exercise02.py<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">print('Enter your name, please:')<br>a = input()<br>print ('Have a nice day, '+&nbsp;a+'.')<\/p>\n\n\n<p>In the third line of the code, we connected three elements with the \"+\" sign: the phrase <em>\"Have a nice day<\/em>\", the variable <em>a<\/em> and the period. <em>Have a nice day<\/em> phrase and the period are in quotation marks. Note&nbsp;that the phrase <em>Have a nice day<\/em> includes a comma and a space at the&nbsp;end:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"174\" height=\"27\" src=\"http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Writing-have-a-nice-day.png\" alt class=\"wp-image-96\"><\/figure>\n\n\n<h2 class=\"fonts-plugin-block \" style=\"font-weight: normal\">A few final remarks<\/h2>\n\n\n<p>We do not enclose variable names in quotation marks. The obligation to use quotation marks applies only to strings. Try to write 'a' instead of a in the last line of our script and see what happens when you run the program.<\/p>\n\n\n\n<p>The variable name does not have to be a single letter. You can use a combination of letters and numbers to name your variables. Do not use spaces or hyphens, but you can use underscore. Letters can be uppercase or lowercase, but it is recommended that you use lowercase letters in your variable names.<\/p>\n\n\n\n<p>Do not start a variable name with a&nbsp;digit.<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">A valid example of a variable name: your_name<\/p>\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">Invalid variable name example: your-name<\/p>\n\n\n<p>You can devote quite a lot of space to the topic of variables and reflect on various really interesting aspects of this topic. Let us mention, for example, the issue of prior declaration of variables \u2014 before we use them in the program, and a deeper analysis of the relationship between the variable and the value assigned to it. As you continue to learn Python, we assure you that you will be interested in these topics yourself. And we promise to come back to&nbsp;them.<\/p>\n\n\n<h2 class=\"fonts-plugin-block \" style=\"font-weight: normal\">For volunteers:<\/h2>\n\n\n<p>When you want to combine several text elements to be printed, in the print command you can concatenate them with the \"+\" sign. Another option is to combine using commas:<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">print('Have a nice day',&nbsp;a,&nbsp;'.')<\/p>\n\n\n<p>Using commas will cause Python to separate items with spaces. Try and compare the effect.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p id=\"Odcinek-3\">Odcinek\u20113<\/p>\n\n\n<h1 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Zmienne w Pythonie<\/h1>\n\n\n<p>Witajcie ponownie.<\/p>\n\n\n\n<p>W obecnym odcinku spr\u00f3bujemy wsp\u00f3lnie napisa\u0107 prosty skrypt w j\u0119zyku Python.<\/p>\n\n\n\n<p>Chcemy aby: 1\/ skrypt poprosi\u0142 u\u017cytkownika o podanie imienia,&nbsp; 2\/ zaczyta\u0142 imi\u0119, kt\u00f3re u\u017cytkownik poda,&nbsp; 3\/ napisa\u0142 na ekranie pozdrowienie: &nbsp;Mi\u0142ego dnia, [imi\u0119].<\/p>\n\n\n\n<p>Dialog pomi\u0119dzy programem a u\u017cytkownikiem wygl\u0105da\u0142 b\u0119dzie zatem np.&nbsp;tak:<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">Program: Podaj twoje imi\u0119, prosz\u0119.<br>U\u017cytkownik: Adam<br>Program: Mi\u0142ego dnia,&nbsp;Adam.<\/p>\n\n\n<p>Skupimy si\u0119 na samym kodzie, bez dodatkowych porad technicznych. Je\u015bli nie wiesz, jak zapisa\u0107 kod&nbsp;w pliku, a p\u00f3\u017aniej go uruchomi\u0107, zajrzyj do poprzedniego postu.<\/p>\n\n\n\n<p>Jak pewnie si\u0119 domy\u015blasz, pewn\u0105 trudno\u015b\u0107 stanowi\u0107 b\u0119dzie u\u017cycie przez program imienia u\u017cytkownika, w generowanym pozdrowieniu &nbsp;Mi\u0142ego dnia [imi\u0119].<\/p>\n\n\n\n<p>Aby program u\u017cy\u0142 w odpowiedzi wcze\u015bniej zaczytane (zapami\u0119tane) imi\u0119 u\u017cytkownika, b\u0119dziemy musieli pos\u0142u\u017cy\u0107 si\u0119 <strong>zmienn\u0105<\/strong>. Zmienne to bardzo wa\u017cny element ka\u017cdego j\u0119zyka programowania. Trudno wyobrazi\u0107 sobie kodowanie, bez zrozumienia czym s\u0105 zmienne i jak dzia\u0142aj\u0105.<\/p>\n\n\n\n<p>No dobrze. Zacznijmy (dla rozgrzewki) od uproszczonej wersji skryptu, aby po uruchomieniu osi\u0105gn\u0105\u0107 taki&nbsp;efekt:<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">Program: Podaj twoje imi\u0119, prosz\u0119.<br>U\u017cytkownik: [imi\u0119]<br>Program: Mi\u0142ego dnia.<\/p>\n\n\n<p>Na razie zatem bez przechwycenia przez program podanego imienia i wstawienia go w trzeciej linii (czyli jeszcze bez u\u017cycia zmiennej).<\/p>\n\n\n\n<p>Taki program potrafimy ju\u017c z \u0142atwo\u015bci\u0105 napisa\u0107.<\/p>\n\n\n\n<p>Oto kod:<\/p>\n\n\n\n<p>Exercise02.py<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">print('Podaj twoje imi\u0119, prosz\u0119:')<br>input()<br>print('Mi\u0142ego dnia.')<\/p>\n\n\n<p>Program prosi u\u017cytkownika o podanie imienia (polecenie print), zapami\u0119tuje je (polecenie input), na&nbsp;koniec generuje pozdrowienie: Mi\u0142ego dnia.<\/p>\n\n\n\n<p>Ale jak zrobi\u0107, aby po s\u0142owach <em>Mi\u0142ego dnia<\/em> program dopisywa\u0142 podane przez u\u017cytkownika imi\u0119? Jak&nbsp;ju\u017c wspomnieli\u015bmy, do tego celu potrzebowa\u0107 b\u0119dziemy zmiennej. C\u00f3\u017c to takiego?<\/p>\n\n\n<h2 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Zmienne w Pythonie<\/h2>\n\n\n<p>W Pythonie zmienna nie jest tym samym, co w matematyce. Zmienna w Pythonie to po prostu <strong>etykietka<\/strong>, kt\u00f3r\u0105 oznakowujesz fragment danych. Wyobra\u017a sobie jaki\u015b \"solidny\" kawa\u0142 danych, np.&nbsp;list\u0119 najwi\u0119kszych miast w Niemczech, kt\u00f3r\u0105 wpisa\u0142e\u015b na samym pocz\u0105tku nowotworzonego skryptu w Pythonie:<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal;font-size: 14px\">&nbsp;&nbsp; ['Aachen', 'Augsburg', 'Bergisch Gladbach', 'Berlin', 'Bielefeld',<br>&nbsp;&nbsp;&nbsp; 'Bochum', 'Bonn', 'Bottrop', 'Braunschweig', 'Bremen',<br>&nbsp;&nbsp;&nbsp; 'Bremerhaven', 'Chemnitz', 'Cologne (K\u00f6ln)',<br>&nbsp;&nbsp;&nbsp; 'Darmstadt', 'Dortmund', 'Dresden', 'Duisburg', 'D\u00fcsseldorf',<br>&nbsp;&nbsp;&nbsp; 'Erfurt', 'Erlangen', 'Essen', 'Frankfurt am Main', 'Freiburg im Breisgau',<br>&nbsp;&nbsp;&nbsp; 'F\u00fcrth', 'Gelsenkirchen', 'G\u00f6ttingen', 'Hagen', 'Halle (Saale)',<br>&nbsp;&nbsp;&nbsp; 'Hamburg', 'Hamm', 'Hanover (Hannover)', 'Heidelberg', 'Heilbronn',<br>&nbsp;&nbsp;&nbsp; 'Herne', 'Hildesheim', 'Ingolstadt', 'Jena', 'Karlsruhe',<br>&nbsp;&nbsp;&nbsp; 'Kassel', 'Kiel', 'Koblenz', 'Krefeld', 'Leipzig',<br>&nbsp;&nbsp;&nbsp; 'Leverkusen', 'L\u00fcbeck', 'Ludwigshafen am Rhein', 'Magdeburg', 'Mainz',<br>&nbsp;&nbsp;&nbsp; 'Mannheim', 'Moers', 'M\u00f6nchengladbach', 'M\u00fclheim an der Ruhr', 'Munich (M\u00fcnchen)',<br>&nbsp;&nbsp;&nbsp; 'M\u00fcnster', 'Neuss', 'Nuremberg (N\u00fcrnberg)', 'Oberhausen', 'Offenbach am Main',<br>&nbsp;&nbsp;&nbsp; 'Oldenburg', 'Osnabr\u00fcck', 'Paderborn', 'Pforzheim', 'Potsdam',<br>&nbsp;&nbsp;&nbsp; 'Recklinghausen', 'Regensburg', 'Remscheid', 'Reutlingen', 'Rostock',<br>&nbsp;&nbsp;&nbsp; 'Saarbr\u00fccken', 'Salzgitter', 'Siegen', 'Solingen', 'Stuttgart',<br>&nbsp;&nbsp;&nbsp; 'Trier', 'Ulm', 'Wiesbaden', 'Wolfsburg', 'Wuppertal', 'W\u00fcrzburg']<br><\/p>\n\n\n<p>W Pythonie na takim \"obiekcie\" jak lista mo\u017cesz wykonywa\u0107 rozmaite <strong><em>polecenia<\/em><\/strong>, drukowa\u0107 t\u0119 list\u0119 na&nbsp;ekranie, sortowa\u0107 j\u0105, usuwa\u0107 wybrane elementy b\u0105d\u017a dodawa\u0107 nowe, \u0142\u0105czy\u0107 z innymi listami itd<a href=\"#_ftn1\">[1]<\/a>.<\/p>\n\n\n\n<p>Jak jednak odwo\u0142a\u0107 si\u0119 w <strong><em>poleceniu<\/em><\/strong> do listy? Jak przywo\u0142a\u0107 list\u0119, o kt\u00f3r\u0105 nam chodzi? Wypisuj\u0105c ca\u0142\u0105 jej zawarto\u015b\u0107? Za ka\u017cdym razem, gdy chcemy zrealizowa\u0107 polecenie?<\/p>\n\n\n\n<p>Przyznasz, \u017ce by\u0142oby to wyj\u0105tkowo m\u0119cz\u0105ce. Czy nie pro\u015bciej nazwa\u0107 t\u0119 list\u0119 jak\u0105\u015b literk\u0105, np. <strong>L<\/strong>? W\u00f3wczas, w dowolnym poleceniu, u\u017cyjemy tej jednej kr\u00f3tkiej literki, zamiast \u017cmudnie wypisywa\u0107 ca\u0142\u0105 zawarto\u015b\u0107 listy. Ta nasza magiczna literka to w\u0142a\u015bnie <strong>zmienna<\/strong>.<\/p>\n\n\n\n<p>W Pythonie zmienna jest zamiennikiem obiektu, do kt\u00f3rego zosta\u0142a przypisana. M\u00f3wimy na ni\u0105 tak\u017ce odno\u015bnik (<em>reference<\/em>).<\/p>\n\n\n\n<p>Przypisanie zmiennej do listy (czy te\u017c dowolnego innego obiektu) wykonujemy poprzez zwyk\u0142y znak r\u00f3wno\u015bci&nbsp;(=)<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal;font-size: 14px\">L = [<br>&nbsp;&nbsp;&nbsp; 'Aachen', 'Augsburg', 'Bergisch Gladbach', 'Berlin', 'Bielefeld',<br>&nbsp;&nbsp;&nbsp; 'Bochum', 'Bonn', 'Bottrop', 'Braunschweig', 'Bremen',<br>&nbsp;&nbsp;&nbsp; 'Bremerhaven', 'Chemnitz', 'Cologne (K\u00f6ln)',<br>&nbsp;&nbsp;&nbsp; 'Darmstadt', 'Dortmund', 'Dresden', 'Duisburg', 'D\u00fcsseldorf',<br>&nbsp;&nbsp;&nbsp; 'Erfurt', 'Erlangen', 'Essen', 'Frankfurt am Main', 'Freiburg im Breisgau',<br>&nbsp;&nbsp;&nbsp; 'F\u00fcrth', 'Gelsenkirchen', 'G\u00f6ttingen', 'Hagen', 'Halle (Saale)',<br>&nbsp;&nbsp;&nbsp; 'Hamburg', 'Hamm', 'Hanover (Hannover)', 'Heidelberg', 'Heilbronn',<br>&nbsp;&nbsp;&nbsp; 'Herne', 'Hildesheim', 'Ingolstadt', 'Jena', 'Karlsruhe',<br>&nbsp;&nbsp;&nbsp; 'Kassel', 'Kiel', 'Koblenz', 'Krefeld', 'Leipzig',<br>&nbsp;&nbsp;&nbsp; 'Leverkusen', 'L\u00fcbeck', 'Ludwigshafen am Rhein', 'Magdeburg', 'Mainz',<br>&nbsp;&nbsp;&nbsp; 'Mannheim', 'Moers', 'M\u00f6nchengladbach', 'M\u00fclheim an der Ruhr', 'Munich (M\u00fcnchen)',<br>&nbsp;&nbsp;&nbsp; 'M\u00fcnster', 'Neuss', 'Nuremberg (N\u00fcrnberg)', 'Oberhausen', 'Offenbach am Main',<br>&nbsp;&nbsp;&nbsp; 'Oldenburg', 'Osnabr\u00fcck', 'Paderborn', 'Pforzheim', 'Potsdam',<br>&nbsp;&nbsp;&nbsp; 'Recklinghausen', 'Regensburg', 'Remscheid', 'Reutlingen', 'Rostock',<br>&nbsp;&nbsp;&nbsp; 'Saarbr\u00fccken', 'Salzgitter', 'Siegen', 'Solingen', 'Stuttgart',<br>&nbsp;&nbsp;&nbsp; 'Trier', 'Ulm', 'Wiesbaden', 'Wolfsburg', 'Wuppertal', 'W\u00fcrzburg'<br>&nbsp;&nbsp;&nbsp; ]<br>&nbsp;&nbsp;&nbsp; <\/p>\n\n\n<p>Sp\u00f3jrzmy, jak b\u0119dzie wygl\u0105da\u0142 skrypt z poleceniem wydrukowania powy\u017cszej listy:<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal;font-size: 14px\">L = [<br>&nbsp;&nbsp;&nbsp; 'Aachen', 'Augsburg', 'Bergisch Gladbach', 'Berlin', 'Bielefeld',<br>&nbsp;&nbsp;&nbsp; 'Bochum', 'Bonn', 'Bottrop', 'Braunschweig', 'Bremen',<br>&nbsp;&nbsp;&nbsp; 'Bremerhaven', 'Chemnitz', 'Cologne (K\u00f6ln)',<br>&nbsp;&nbsp;&nbsp; 'Darmstadt', 'Dortmund', 'Dresden', 'Duisburg', 'D\u00fcsseldorf',<br>&nbsp;&nbsp;&nbsp; 'Erfurt', 'Erlangen', 'Essen', 'Frankfurt am Main', 'Freiburg im Breisgau',<br>&nbsp;&nbsp;&nbsp; 'F\u00fcrth', 'Gelsenkirchen', 'G\u00f6ttingen', 'Hagen', 'Halle (Saale)',<br>&nbsp;&nbsp;&nbsp; 'Hamburg', 'Hamm', 'Hanover (Hannover)', 'Heidelberg', 'Heilbronn',<br>&nbsp;&nbsp;&nbsp; 'Herne', 'Hildesheim', 'Ingolstadt', 'Jena', 'Karlsruhe',<br>&nbsp;&nbsp;&nbsp; 'Kassel', 'Kiel', 'Koblenz', 'Krefeld', 'Leipzig',<br>&nbsp;&nbsp;&nbsp; 'Leverkusen', 'L\u00fcbeck', 'Ludwigshafen am Rhein', 'Magdeburg', 'Mainz',<br>&nbsp;&nbsp;&nbsp; 'Mannheim', 'Moers', 'M\u00f6nchengladbach', 'M\u00fclheim an der Ruhr', 'Munich (M\u00fcnchen)',<br>&nbsp;&nbsp;&nbsp; 'M\u00fcnster', 'Neuss', 'Nuremberg (N\u00fcrnberg)', 'Oberhausen', 'Offenbach am Main',<br>&nbsp;&nbsp;&nbsp; 'Oldenburg', 'Osnabr\u00fcck', 'Paderborn', 'Pforzheim', 'Potsdam',<br>&nbsp;&nbsp;&nbsp; 'Recklinghausen', 'Regensburg', 'Remscheid', 'Reutlingen', 'Rostock',<br>&nbsp;&nbsp;&nbsp; 'Saarbr\u00fccken', 'Salzgitter', 'Siegen', 'Solingen', 'Stuttgart',<br>&nbsp;&nbsp;&nbsp; 'Trier', 'Ulm', 'Wiesbaden', 'Wolfsburg', 'Wuppertal', 'W\u00fcrzburg'<br>&nbsp;&nbsp;&nbsp; ]<br><br>print(L)<\/p>\n\n\n<p>\/wynikiem b\u0119dzie tre\u015b\u0107 listy wydrukowana na ekranie twojego komputera\/<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Jak widzisz, w poleceniu print skorzystali\u015bmy z kr\u00f3tkiej jednoliterowej zmiennej, aby wydrukowa\u0107&nbsp; ca\u0142\u0105 d\u0142ug\u0105, wieloelementow\u0105 list\u0119.<\/p>\n\n\n\n<p>Gdyby\u015bmy nie skorzystali ze zmiennej, nasz skrypt wygl\u0105da\u0142by tak:<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal;font-size: 14px\">[<br>&nbsp;&nbsp;&nbsp; 'Aachen', 'Augsburg', 'Bergisch Gladbach', 'Berlin', 'Bielefeld',<br>&nbsp;&nbsp;&nbsp; 'Bochum', 'Bonn', 'Bottrop', 'Braunschweig', 'Bremen',<br>&nbsp;&nbsp;&nbsp; 'Bremerhaven', 'Chemnitz', 'Cologne (K\u00f6ln)',<br>&nbsp;&nbsp;&nbsp; 'Darmstadt', 'Dortmund', 'Dresden', 'Duisburg', 'D\u00fcsseldorf',<br>&nbsp;&nbsp;&nbsp; 'Erfurt', 'Erlangen', 'Essen', 'Frankfurt am Main', 'Freiburg im Breisgau',<br>&nbsp;&nbsp;&nbsp; 'F\u00fcrth', 'Gelsenkirchen', 'G\u00f6ttingen', 'Hagen', 'Halle (Saale)',<br>&nbsp;&nbsp;&nbsp; 'Hamburg', 'Hamm', 'Hanover (Hannover)', 'Heidelberg', 'Heilbronn',<br>&nbsp;&nbsp;&nbsp; 'Herne', 'Hildesheim', 'Ingolstadt', 'Jena', 'Karlsruhe',<br>&nbsp;&nbsp;&nbsp; 'Kassel', 'Kiel', 'Koblenz', 'Krefeld', 'Leipzig',<br>&nbsp;&nbsp;&nbsp; 'Leverkusen', 'L\u00fcbeck', 'Ludwigshafen am Rhein', 'Magdeburg', 'Mainz',<br>&nbsp;&nbsp;&nbsp; 'Mannheim', 'Moers', 'M\u00f6nchengladbach', 'M\u00fclheim an der Ruhr', 'Munich (M\u00fcnchen)',<br>&nbsp;&nbsp;&nbsp; 'M\u00fcnster', 'Neuss', 'Nuremberg (N\u00fcrnberg)', 'Oberhausen', 'Offenbach am Main',<br>&nbsp;&nbsp;&nbsp; 'Oldenburg', 'Osnabr\u00fcck', 'Paderborn', 'Pforzheim', 'Potsdam',<br>&nbsp;&nbsp;&nbsp; 'Recklinghausen', 'Regensburg', 'Remscheid', 'Reutlingen', 'Rostock',<br>&nbsp;&nbsp;&nbsp; 'Saarbr\u00fccken', 'Salzgitter', 'Siegen', 'Solingen', 'Stuttgart',<br>&nbsp;&nbsp;&nbsp; 'Trier', 'Ulm', 'Wiesbaden', 'Wolfsburg', 'Wuppertal', 'W\u00fcrzburg'<br>&nbsp;&nbsp;&nbsp; ]<br>print([<br>&nbsp;&nbsp;&nbsp; 'Aachen', 'Augsburg', 'Bergisch Gladbach', 'Berlin', 'Bielefeld',<br>&nbsp;&nbsp;&nbsp; 'Bochum', 'Bonn', 'Bottrop', 'Braunschweig', 'Bremen',<br>&nbsp;&nbsp;&nbsp; 'Bremerhaven', 'Chemnitz', 'Cologne (K\u00f6ln)',<br>&nbsp;&nbsp;&nbsp; 'Darmstadt', 'Dortmund', 'Dresden', 'Duisburg', 'D\u00fcsseldorf',<br>&nbsp;&nbsp;&nbsp; 'Erfurt', 'Erlangen', 'Essen', 'Frankfurt am Main', 'Freiburg im Breisgau',<br>&nbsp;&nbsp;&nbsp; 'F\u00fcrth', 'Gelsenkirchen', 'G\u00f6ttingen', 'Hagen', 'Halle (Saale)',<br>&nbsp;&nbsp;&nbsp; 'Hamburg', 'Hamm', 'Hanover (Hannover)', 'Heidelberg', 'Heilbronn',<br>&nbsp;&nbsp;&nbsp; 'Herne', 'Hildesheim', 'Ingolstadt', 'Jena', 'Karlsruhe',<br>&nbsp;&nbsp;&nbsp; 'Kassel', 'Kiel', 'Koblenz', 'Krefeld', 'Leipzig',<br>&nbsp;&nbsp;&nbsp; 'Leverkusen', 'L\u00fcbeck', 'Ludwigshafen am Rhein', 'Magdeburg', 'Mainz',<br>&nbsp;&nbsp; &nbsp;'Mannheim', 'Moers', 'M\u00f6nchengladbach', 'M\u00fclheim an der Ruhr', 'Munich (M\u00fcnchen)',<br>&nbsp;&nbsp;&nbsp; 'M\u00fcnster', 'Neuss', 'Nuremberg (N\u00fcrnberg)', 'Oberhausen', 'Offenbach am Main',<br>&nbsp;&nbsp;&nbsp; 'Oldenburg', 'Osnabr\u00fcck', 'Paderborn', 'Pforzheim', 'Potsdam',<br>&nbsp;&nbsp;&nbsp; 'Recklinghausen', 'Regensburg', 'Remscheid', 'Reutlingen', 'Rostock',<br>&nbsp;&nbsp;&nbsp; 'Saarbr\u00fccken', 'Salzgitter', 'Siegen', 'Solingen', 'Stuttgart',<br>&nbsp;&nbsp;&nbsp; 'Trier', 'Ulm', 'Wiesbaden', 'Wolfsburg', 'Wuppertal', 'W\u00fcrzburg'<br>&nbsp;&nbsp;&nbsp; ])<\/p>\n\n\n<p>\/wynikiem b\u0119dzie tre\u015b\u0107 listy wydrukowana na ekranie twojego komputera\/<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Brak posi\u0142kowania si\u0119 zmienn\u0105 oznacza (jak wida\u0107) konieczno\u015b\u0107 przepisania ca\u0142ej d\u0142ugiej listy w&nbsp;poleceniu print, pomimo \u017ce powy\u017cej lista ta by\u0142a ju\u017c raz wpisana do&nbsp;kodu.<\/p>\n\n\n\n<p>Nietrudno sobie wyobrazi\u0107, jak wygl\u0105da\u0142by tw\u00f3j skrypt, gdyby\u015b chcia\u0142 wprowadzi\u0107 wi\u0119cej polece\u0144 dotycz\u0105cych tej listy. Lista by\u0142aby przepisywana ogromn\u0105 ilo\u015b\u0107 razy, a skrypt by\u0142by pora\u017caj\u0105co d\u0142ugi i&nbsp;m\u0119cz\u0105cy w czytaniu.<\/p>\n\n\n\n<p>Zmienna zatem ratuje nas przed bezrefleksyjnym i nu\u017c\u0105cym powielaniem kodu.<\/p>\n\n\n\n<p>Ale czy&nbsp;tylko?<\/p>\n\n\n<h2 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Zmienna przechwytuj\u0105ca<\/h2>\n\n\n<p>Zmienna daje jeszcze co\u015b wi\u0119cej, ponad to co napisali\u015bmy przed chwil\u0105. Pozwala odwo\u0142a\u0107 si\u0119 do takich element\u00f3w programu, do kt\u00f3rych nie ma sposobu odwo\u0142ania si\u0119 w \u017caden inny spos\u00f3b. Mamy na my\u015bli dane wprowadzane przez u\u017cytkownika podczas uruchomienia programu \u2013 jak w naszym przypadku \"imi\u0119\". Nie jeste\u015bmy przecie\u017c w \u017caden spos\u00f3b w stanie przewidzie\u0107, jakie imi\u0119 wpisze u\u017cytkownik, gdy&nbsp;program go o to poprosi. R\u00f3wnie dobrze mo\u017ce to by\u0107 imi\u0119 Adam, jak i Thomas albo&nbsp;Sara.<\/p>\n\n\n\n<p>Dane wprowadzane przez u\u017cytkownika nie s\u0105 fragmentem kodu. S\u0105 to dane wprowadzane \"z zewn\u0105trz\" w fazie uruchomieniowej programu. Dlatego nazywa si\u0119 je <em>run-time data<\/em>, a cz\u0119sto tak\u017ce <em>transient data<\/em> (dane ulotne), gdy\u017c wraz z zamkni\u0119ciem programu ulatuj\u0105 w niebyt<a href=\"#_ftn2\">[2]<\/a>.<\/p>\n\n\n\n<p>M\u00f3wi\u0105c bardzo prostym j\u0119zykiem: dane wprowadzone w fazie dzia\u0142ania programu musisz jako\u015b nazwa\u0107, aby m\u00f3c si\u0119 do nich nast\u0119pnie odwo\u0142a\u0107.<\/p>\n\n\n\n<p>A m\u00f3wi\u0105c troch\u0119 bardziej profesjonalnym j\u0119zykiem: dane te trzeba przypisa\u0107 do zmiennej, aby m\u00f3c si\u0119 do nich nast\u0119pnie odwo\u0142a\u0107.<\/p>\n\n\n\n<p>Przypisanie zmiennej odbywa si\u0119 identycznie, jak we wcze\u015bniej przytoczonym przyk\u0142adzie listy. Wybierasz sobie dowoln\u0105 literk\u0119 alfabetu, powiedzmy \"a\" i przyporz\u0105dkowujesz j\u0105 do wprowa\u00addzonych danych:<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">a =&nbsp;input()<\/p>\n\n\n<p>Dane, kt\u00f3re u\u017cytkownik wprowadza do programu, oznaczyli\u015bmy jako&nbsp;a.<\/p>\n\n\n\n<p>Teraz z \u0142atwo\u015bci\u0105 mo\u017cemy nakaza\u0107 programowi, aby wydrukowa\u0142 te&nbsp;dane:<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">print(a)<\/p>\n\n\n<p>Proste i \u0142atwe, prawda?<\/p>\n\n\n\n<p>Mo\u017cemy wreszcie wr\u00f3ci\u0107 do naszego skryptu z pocz\u0105tku artyku\u0142u, i&nbsp;zaprezentowa\u0107 jego pe\u0142n\u0105 posta\u0107:<\/p>\n\n\n\n<p>Exercise02.py<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">print('Podaj twoje imi\u0119, prosz\u0119:')<br>a = input()<br>print('Mi\u0142ego dnia, '+a+'.')<\/p>\n\n\n<p>W trzeciej linijce kodu po\u0142\u0105czyli\u015bmy znakiem \"+\" trzy elementy: fraz\u0119 &nbsp;Mi\u0142ego dnia, &nbsp;zmienn\u0105 a oraz kropk\u0119. Fraza Mi\u0142ego dnia i kropka uj\u0119te s\u0105 w cudzys\u0142\u00f3w.<\/p>\n\n\n\n<p>Zauwa\u017c \u017ce fraza Mi\u0142ego dnia obejmuje przecinek oraz spacj\u0119 na jej&nbsp;ko\u0144cu:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"174\" height=\"27\" src=\"http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Writing-have-a-nice-day.png\" alt class=\"wp-image-96\"><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Dla ch\u0119tnych:<\/strong> Gdy w poleceniu print zamierzasz po\u0142\u0105czy\u0107 kilka element\u00f3w tekstowych, kt\u00f3re maj\u0105 by\u0107 wydrukowane, mo\u017cesz \u0142\u0105czy\u0107 je w\u0142a\u015bnie znakiem \"+\". Inn\u0105 opcj\u0105 jest \u0142\u0105czenie z wykorzystaniem przecink\u00f3w: print('Mi\u0142ego dnia', a, '.') U\u017cycie przecink\u00f3w b\u0119dzie mia\u0142o ten skutek, \u017ce Python rozdzieli elementy spacjami. Wypr\u00f3buj i por\u00f3wnaj efekt.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n<h2 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Par\u0119 uwag na zako\u0144czenie<\/h2>\n\n\n<p>Nazw zmiennych nie ujmujemy w cudzys\u0142\u00f3w. Obowi\u0105zek stosowania cudzys\u0142owu dotyczy jedynie \u0142a\u0144cuch\u00f3w znak\u00f3w. Spr\u00f3buj w ostatniej linijce kodu naszego skryptu napisa\u0107 'a' zamiast a i&nbsp;zobacz, co si\u0119 stanie, gdy uruchomisz program.<\/p>\n\n\n\n<p>Nazwa zmiennej nie musi by\u0107 pojedyncz\u0105 literk\u0105. Mo\u017cesz u\u017cywa\u0107 kombinacji liter i cyfr dla nazywania swoich zmiennych. Nie u\u017cywaj spacji ani my\u015blnik\u00f3w, natomiast mo\u017cesz u\u017cy\u0107 podkre\u015blnika (underscore). Litery mog\u0105 by\u0107 du\u017ce lub ma\u0142e, ale rekomenduje si\u0119 u\u017cywanie ma\u0142ych liter w nazwach zmiennych.<\/p>\n\n\n\n<p>Nie rozpoczynaj nazwy zmiennej od&nbsp;cyfry.<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">Poprawny przyk\u0142ad nazwy zmiennej: &nbsp;your_name<\/p>\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">Niepoprawny przyk\u0142ad nazwy zmiennej: &nbsp;your-name<\/p>\n\n\n<p>Tematowi zmiennych mo\u017cna po\u015bwi\u0119ci\u0107 ca\u0142kiem sporo miejsca i pozastanawia\u0107 si\u0119 nad r\u00f3\u017cnymi naprawd\u0119 ciekawymi aspektami tego tematu. Wspomnijmy cho\u0107by o kwestii uprzedniego deklaro\u00adwania zmiennych \u2013 zanim u\u017cyjemy ich w programie oraz o g\u0142\u0119bszej analizie relacji pomi\u0119dzy zmienn\u0105 a przypisan\u0105 do niej warto\u015bci\u0105. Gdy b\u0119dziesz kontynuowa\u0142 nauk\u0119 Pythona, zapewniamy, \u017ce sam z zainteresowaniem si\u0119gniesz po te tematy. A my obiecujemy do nich wr\u00f3ci\u0107.<\/p>\n\n\n\n<hr class=\"wp-block-separator\">\n\n\n\n<p><a href=\"#_ftnref1\">[1]<\/a> nie przejmuj si\u0119 tym, \u017ce, prezentujemy przyk\u0142ad listy, cho\u0107 jeszcze nie om\u00f3wili\u015bmy list w Pythonie; na ten moment wystarczy nam zupe\u0142nie intuicyjne pojmowanie, czym jest&nbsp;lista;<\/p>\n\n\n\n<p><a href=\"#_ftnref2\">[2]<\/a> Dane ulotne s\u0105 przechowywane w pami\u0119ci RAM komputera (pami\u0119\u0107 o dost\u0119pie swobodnym). Po zako\u0144czeniu programu dane s\u0105 usuwane (unicestwiane). Transient data is stored in the computers RAM (random access memory). When a program completes, the data is de-allocated (it&nbsp;dies).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello again. In this passage, we'll try to write a simple Python script together. We&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\/95"}],"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=95"}],"version-history":[{"count":7,"href":"http:\/\/juthon.com\/index.php\/wp-json\/wp\/v2\/posts\/95\/revisions"}],"predecessor-version":[{"id":176,"href":"http:\/\/juthon.com\/index.php\/wp-json\/wp\/v2\/posts\/95\/revisions\/176"}],"wp:attachment":[{"href":"http:\/\/juthon.com\/index.php\/wp-json\/wp\/v2\/media?parent=95"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juthon.com\/index.php\/wp-json\/wp\/v2\/categories?post=95"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juthon.com\/index.php\/wp-json\/wp\/v2\/tags?post=95"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}