<center></center> <br/> In the past episode we learned the absolute basics how to execute the script and how to print out (display) text in the terminal: * https://steemit.com/programming/@profitgenerator/learn-basic-python-programming-ep-1 In this episode we are going to learn how to use variables and how to interact wit the script, by inputting things from the keyboard. <BR/> ----------------------------- <BR/> # Variables In python you don't need to declare variables (str,int,float,double) like in C++, the code itself automatically detects what kind of variable it is, so it's very easy to use for novices. Essentially you can transform or specify later on, but not always needed. Let's go back to our `test.py` file, and see how to use variables. Well basically you just write the variable name, an `=` and the value of the variable. For numbers you just write the number , for a string variable, like a text, you just write it in the string wraps `"` or `'` we learned in the past episode.  Which will display:  So we have just substituted the argument of the `print` with a variable, instead of writing `print "text"` we just use the variable `text_variable` to contain the data `"text"` and we print it out this way. Also note that a number can be a `string` too however if we put the number in the `""` wraps it becomes a string variable, and only string operations can be done with it like concatenation, trimming ,separation into characters and such. While an integer can only be used mathematically, like addition, division ,etc... It may display the same value, but it's a different kind of variable used for different purposes:  Displaying:  <BR/> ----------------------------- <BR/> # Math Operations Well you just use the basic operators `+ * - / ` just like in any other math software. Then give value to your variables that you want to calculate from, and perform the operation. It would work just fine, obviously you can't divide by 0 and things like that, but otherwise it should work fine.  Displaying:  It works just fine, except for division, it's because all math variables are by default integers I believe, and a division, unless it's a round number, will always give out fractions, which is a [float variable](https://en.wikipedia.org/wiki/Floating-point_arithmetic) with decimal points after it. In python we use the `float()` function to turn the number into a float variable, but then we have to transform each variable into float, or we make the initial variable float by adding a `.0` decimal to the number. So either like this:  Or like this:  Both displaying the correct values this time:  <BR/> ----------------------------- <BR/> # Inputs Now let's interact with the script, let's input the values of A and B directly from the keyboard from the terminal. For this we are going to use the `raw_input()` function. Every time this is called, the terminal will be waiting for a text input. So you write something in the terminal, and you finalize it by hitting `Enter` on the keyboard. So in this case the `raw_input()`will be called 2 times separately for A and B, which means that you will have to enter a number, then hit enter, and then enter another number. The order is as specified in the script, in our case the first one is A. We also have to transform it into `float()` since the `raw_input()` is a string based function.  Of course B can't be 0, we can't divide by 0, and you can't enter a text, otherwise it will be an error. So just enter 2 numbers:  So I have entered 56 for A, hit enter and 14 for B and hit enter. Then it displayed the results of those calculations. **And hence this is how you created your very first calculator software, it's just this easy!** ### Essentially, let's make it more user friendly and add instructions inside it:  Note that we can't combine float variable with string, so we have to turn the float into string with the `str()` function. And adding together 2 strings is called [concatenation](https://en.wikipedia.org/wiki/Concatenation) which is done with `+` operator. It means that the 2nd string will be put after the end of the first one.  ### So that is how you have your first calculator ready to calculate for you. Stay tuned for the next episode, I will be teaching you even more interesting stuff about programming! ------------------------------------------ **Sources:** * https://pixabay.com * Python is a trademark of the [Python Software Foundation](https://www.python.org/psf/) ------------------------------------------- <CENTER><H1>Upvote, ReSteem & <a href="https://steemit.com/@profitgenerator" target='_blank'><img src='https://s4.postimg.org/cfz9b1mnh/bluebutton.png' border='0' alt='bluebutton'/></a></H1> </CENTER>
author | profitgenerator |
---|---|
permlink | learn-basic-python-programming-ep-2-variables-and-inputs |
category | programming |
json_metadata | {"tags":["programming","howto","education","python","tutorial"],"image":["https://steemitimages.com/DQmU3VQGGHcbE5JyZBGTF8iN1d1xustawjxytWr4bP5kMNt/p1.png","https://steemitimages.com/DQmVPyXa3zxcUZ3M1inRrd4j5zcjrQXDwU7NM9ob8bBTPsY/1.png","https://steemitimages.com/DQmVvzrZm56vx1NCdeEEgWqzPut2pbsNyG3tyH2iFAD1TMU/2.png","https://steemitimages.com/DQmV2bg7yYxr49Hhz8M3njwjzk47kssMqiNUj3c5P9dAXut/T2.png","https://steemitimages.com/DQma2zqu99kynEfWVkakPW3qpdyS39drTdLZe8vpaus9wqD/T1.png","https://steemitimages.com/DQmdCoiq3eG1W4Pm1AwLSJK1yv7AHs8ucBbnTvELTvpasvg/M1.png","https://steemitimages.com/DQmexutbV5XuQ1SQYYj4E5nBgzALsVJoXVgQQ9mvzwfAAUs/M2.png","https://steemitimages.com/DQmebk67m9NQiDtWBHrD67YyvUbZDAWsBLWgHRVPVAJKyMo/f1.png","https://steemitimages.com/DQmec55zJahHrifgQY3s78ELPBqiwnMAVDzPTaRXjcLKy8e/f2.png","https://steemitimages.com/DQmNSqpZuFzTirLMRzJALdGmPNqb12NAoVc1MTenzUoJKc4/f3.png","https://steemitimages.com/DQmNdjJg5X3jKW99Wnhov6VWoN1R8Xub52SgTWGuLfCcVCc/r1.png","https://steemitimages.com/DQmSq57TX7bwQ8EhdUZbdsvLvJAT41XaPzf5DcqTqJ37PsW/r2.png","https://steemitimages.com/DQmSkFy3DxnCPx1TgKWgciYbx7Vo8iLYWz4eEwjPPv7osvw/ca1.png","https://steemitimages.com/DQmUsfo8ATB42RK7xAbaSxkfGmZfNQS6optewzVFNFqnN9t/ca2.png","https://s4.postimg.org/cfz9b1mnh/bluebutton.png"],"links":["https://steemit.com/programming/@profitgenerator/learn-basic-python-programming-ep-1","https://en.wikipedia.org/wiki/Floating-point_arithmetic","https://en.wikipedia.org/wiki/Concatenation","https://pixabay.com","https://www.python.org/psf/","https://steemit.com/@profitgenerator"],"app":"steemit/0.1","format":"markdown"} |
created | 2017-06-30 11:24:48 |
last_update | 2017-06-30 11:24:48 |
depth | 0 |
children | 13 |
last_payout | 2017-07-07 11:24:48 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 6.415 HBD |
curator_payout_value | 1.442 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 6,056 |
author_reputation | 68,549,319,463,075 |
root_title | "Learn Basic Python Programming EP.2. - Variables & Inputs" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 6,748,637 |
net_rshares | 896,351,168,591 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
sandra | 0 | 13,901,011,729 | 6% | ||
ihashfury | 0 | 2,182,888,386 | 2.16% | ||
jason | 0 | 7,769,178,107 | 2.16% | ||
angusleung100 | 0 | 1,958,027,705 | 100% | ||
michaellamden68 | 0 | 3,711,234,600 | 10% | ||
vortac | 0 | 712,605,369,350 | 25% | ||
dumar022 | 0 | 26,830,397,763 | 14% | ||
chinese-gandalf | 0 | 1,149,343,700 | 100% | ||
stiant | 0 | 19,005,513,194 | 100% | ||
penguinpablo | 0 | 36,655,492,971 | 3% | ||
profitgenerator | 0 | 23,332,553,163 | 100% | ||
saamychristen | 0 | 13,776,443,339 | 50% | ||
freebornangel | 0 | 2,218,006,027 | 4% | ||
sellergenius | 0 | 784,698,790 | 25% | ||
steemcollator | 0 | 7,013,380,738 | 22% | ||
corvuscoraxx | 0 | 461,817,109 | 18.15% | ||
mariaalmeida | 0 | 1,475,574,013 | 50% | ||
scozky | 0 | 1,316,958,821 | 100% | ||
aismor | 0 | 613,570,126 | 100% | ||
kskarthik | 0 | 591,629,235 | 100% | ||
sacred-agent | 0 | 3,653,856,590 | 9% | ||
novaatebatman | 0 | 5,262,007,459 | 100% | ||
altcointrends | 0 | 67,096,080 | 1% | ||
freethink | 0 | 623,797,357 | 100% | ||
saadmmirza | 0 | 164,914,831 | 100% | ||
skela | 0 | 958,295,364 | 100% | ||
btcunchained | 0 | 4,104,987,465 | 100% | ||
sarah.jsd | 0 | 1,160,695,128 | 100% | ||
rythaman | 0 | 766,056,870 | 100% | ||
ragequit | 0 | 870,517,990 | 100% | ||
rmb | 0 | 1,120,065,562 | 100% | ||
pro20 | 0 | 245,789,029 | 100% |
Beauty of python... No need to declare variables; just use it on the go βΊοΈπ
author | btcunchained |
---|---|
permlink | re-profitgenerator-learn-basic-python-programming-ep-2-variables-and-inputs-20170630t121915694z |
category | programming |
json_metadata | {"tags":["programming"],"app":"steemit/0.1"} |
created | 2017-06-30 12:19:24 |
last_update | 2017-06-30 12:19:24 |
depth | 1 |
children | 5 |
last_payout | 2017-07-07 12:19:24 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 75 |
author_reputation | 490,718,345,731 |
root_title | "Learn Basic Python Programming EP.2. - Variables & Inputs" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 6,753,512 |
net_rshares | 893,731,963 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
patriot-thoughts | 0 | 893,731,963 | 100% |
A double edged sword kind of beauty. I come from C++ world so I'm a little biased. lol. C++ was my first. Definitely easier for beginners.
author | patriot-thoughts |
---|---|
permlink | re-btcunchained-re-profitgenerator-learn-basic-python-programming-ep-2-variables-and-inputs-20170630t232321717z |
category | programming |
json_metadata | {"tags":["programming"],"app":"steemit/0.1"} |
created | 2017-06-30 23:23:12 |
last_update | 2017-06-30 23:23:12 |
depth | 2 |
children | 4 |
last_payout | 2017-07-07 23:23:12 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.032 HBD |
curator_payout_value | 0.009 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 138 |
author_reputation | 24,098,233,643 |
root_title | "Learn Basic Python Programming EP.2. - Variables & Inputs" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 6,824,175 |
net_rshares | 5,102,205,642 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
btcunchained | 0 | 5,102,205,642 | 100% |
Hum I have studied both... I think if you were exposed to python first, you will feel python is easy.. Likewise with cpp βΊοΈ
author | btcunchained |
---|---|
permlink | re-patriot-thoughts-re-btcunchained-re-profitgenerator-learn-basic-python-programming-ep-2-variables-and-inputs-20170701t055152671z |
category | programming |
json_metadata | {"tags":["programming"],"app":"steemit/0.1"} |
created | 2017-07-01 05:51:57 |
last_update | 2017-07-01 05:51:57 |
depth | 3 |
children | 3 |
last_payout | 2017-07-08 05:51:57 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 123 |
author_reputation | 490,718,345,731 |
root_title | "Learn Basic Python Programming EP.2. - Variables & Inputs" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 6,853,182 |
net_rshares | 818,287,057 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
patriot-thoughts | 0 | 818,287,057 | 100% |
You seem to be teaching Python 2.x. That's probably not a great idea since the backwards compatibility is broken since Python 3 and everyone is either in process of migrating to Python 3 or done doing so. For anyone interested in great python documentation I suggest starting with [David Beazley](http://www.dabeaz.com/). His presentations are incredibly funny and informative, and his books are pure gold. I can appreciate the amount of effort that went in this guide and I'm not trying to put you down or anything, keep up the work and best of luck!
author | ragequit |
---|---|
permlink | re-profitgenerator-learn-basic-python-programming-ep-2-variables-and-inputs-20170630t113534149z |
category | programming |
json_metadata | {"tags":["programming"],"links":["http://www.dabeaz.com/"],"app":"steemit/0.1"} |
created | 2017-06-30 11:35:36 |
last_update | 2017-06-30 11:35:36 |
depth | 1 |
children | 6 |
last_payout | 2017-07-07 11:35:36 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.198 HBD |
curator_payout_value | 0.003 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 553 |
author_reputation | 84,968,448,410 |
root_title | "Learn Basic Python Programming EP.2. - Variables & Inputs" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 6,749,542 |
net_rshares | 23,332,553,163 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
profitgenerator | 0 | 23,332,553,163 | 100% |
Yes it's true, but it's easier to learn. I am also using mostly Python 3 these days, but for simplicity python 2 is also very useful.
author | profitgenerator |
---|---|
permlink | re-ragequit-re-profitgenerator-learn-basic-python-programming-ep-2-variables-and-inputs-20170630t113745900z |
category | programming |
json_metadata | {"tags":["programming"],"app":"steemit/0.1"} |
created | 2017-06-30 11:37:54 |
last_update | 2017-06-30 11:37:54 |
depth | 2 |
children | 3 |
last_payout | 2017-07-07 11:37:54 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.196 HBD |
curator_payout_value | 0.007 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 133 |
author_reputation | 68,549,319,463,075 |
root_title | "Learn Basic Python Programming EP.2. - Variables & Inputs" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 6,749,736 |
net_rshares | 23,332,553,163 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
profitgenerator | 0 | 23,332,553,163 | 100% |
Nice tut! `raw_input()`is replaced with `input()` in py3, right?
author | kskarthik |
---|---|
permlink | re-profitgenerator-re-ragequit-re-profitgenerator-learn-basic-python-programming-ep-2-variables-and-inputs-20170630t114326341z |
category | programming |
json_metadata | {"tags":["programming"],"app":"steemit/0.1"} |
created | 2017-06-30 11:43:30 |
last_update | 2017-06-30 11:43:30 |
depth | 3 |
children | 2 |
last_payout | 2017-07-07 11:43:30 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 66 |
author_reputation | 220,092,999,236 |
root_title | "Learn Basic Python Programming EP.2. - Variables & Inputs" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 6,750,255 |
net_rshares | 581,745,615 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
kskarthik | 0 | 581,745,615 | 100% |
Actually I will transition to python 3, no need to teach people depreciated stuff!
author | profitgenerator |
---|---|
permlink | re-ragequit-re-profitgenerator-learn-basic-python-programming-ep-2-variables-and-inputs-20170630t114238700z |
category | programming |
json_metadata | {"tags":["programming"],"app":"steemit/0.1"} |
created | 2017-06-30 11:42:54 |
last_update | 2017-06-30 11:42:54 |
depth | 2 |
children | 1 |
last_payout | 2017-07-07 11:42:54 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 82 |
author_reputation | 68,549,319,463,075 |
root_title | "Learn Basic Python Programming EP.2. - Variables & Inputs" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 6,750,193 |
net_rshares | 0 |
I personally think it's for the best :) Can't wait to check it out, i'm subbed! Oh and if I could make one last suggestion it would be to have the code selectable. Good luck!
author | ragequit |
---|---|
permlink | re-profitgenerator-re-ragequit-re-profitgenerator-learn-basic-python-programming-ep-2-variables-and-inputs-20170630t114741748z |
category | programming |
json_metadata | {"tags":["programming"],"app":"steemit/0.1"} |
created | 2017-06-30 11:47:42 |
last_update | 2017-06-30 11:47:42 |
depth | 3 |
children | 0 |
last_payout | 2017-07-07 11:47:42 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 176 |
author_reputation | 84,968,448,410 |
root_title | "Learn Basic Python Programming EP.2. - Variables & Inputs" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 6,750,617 |
net_rshares | 0 |