create account

Learn Basic Python Programming EP.2. - Variables & Inputs by profitgenerator

View this thread on: hive.blogpeakd.comecency.com
· @profitgenerator ·
$7.86
Learn Basic Python Programming EP.2. - Variables & Inputs
<center>![p1.png](https://steemitimages.com/DQmU3VQGGHcbE5JyZBGTF8iN1d1xustawjxytWr4bP5kMNt/p1.png)</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.

![1.png](https://steemitimages.com/DQmVPyXa3zxcUZ3M1inRrd4j5zcjrQXDwU7NM9ob8bBTPsY/1.png)

Which will display:

![2.png](https://steemitimages.com/DQmVvzrZm56vx1NCdeEEgWqzPut2pbsNyG3tyH2iFAD1TMU/2.png)

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:

![T2.png](https://steemitimages.com/DQmV2bg7yYxr49Hhz8M3njwjzk47kssMqiNUj3c5P9dAXut/T2.png)

Displaying:

![T1.png](https://steemitimages.com/DQma2zqu99kynEfWVkakPW3qpdyS39drTdLZe8vpaus9wqD/T1.png)


<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.

![M1.png](https://steemitimages.com/DQmdCoiq3eG1W4Pm1AwLSJK1yv7AHs8ucBbnTvELTvpasvg/M1.png)

Displaying:

![M2.png](https://steemitimages.com/DQmexutbV5XuQ1SQYYj4E5nBgzALsVJoXVgQQ9mvzwfAAUs/M2.png)

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:


![f1.png](https://steemitimages.com/DQmebk67m9NQiDtWBHrD67YyvUbZDAWsBLWgHRVPVAJKyMo/f1.png)

Or like this:

![f2.png](https://steemitimages.com/DQmec55zJahHrifgQY3s78ELPBqiwnMAVDzPTaRXjcLKy8e/f2.png)

Both displaying the correct values this time:

![f3.png](https://steemitimages.com/DQmNSqpZuFzTirLMRzJALdGmPNqb12NAoVc1MTenzUoJKc4/f3.png)


<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.


![r1.png](https://steemitimages.com/DQmNdjJg5X3jKW99Wnhov6VWoN1R8Xub52SgTWGuLfCcVCc/r1.png)


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:

![r2.png](https://steemitimages.com/DQmSq57TX7bwQ8EhdUZbdsvLvJAT41XaPzf5DcqTqJ37PsW/r2.png)

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:

![ca1.png](https://steemitimages.com/DQmSkFy3DxnCPx1TgKWgciYbx7Vo8iLYWz4eEwjPPv7osvw/ca1.png)

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.

![ca2.png](https://steemitimages.com/DQmUsfo8ATB42RK7xAbaSxkfGmZfNQS6optewzVFNFqnN9t/ca2.png)

### 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>
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorprofitgenerator
permlinklearn-basic-python-programming-ep-2-variables-and-inputs
categoryprogramming
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"}
created2017-06-30 11:24:48
last_update2017-06-30 11:24:48
depth0
children13
last_payout2017-07-07 11:24:48
cashout_time1969-12-31 23:59:59
total_payout_value6.415 HBD
curator_payout_value1.442 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6,056
author_reputation68,549,319,463,075
root_title"Learn Basic Python Programming EP.2. - Variables & Inputs"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id6,748,637
net_rshares896,351,168,591
author_curate_reward""
vote details (32)
@btcunchained ·
Beauty of python... No need to declare variables; just use it on the go β˜ΊοΈπŸ‘
πŸ‘  
properties (23)
authorbtcunchained
permlinkre-profitgenerator-learn-basic-python-programming-ep-2-variables-and-inputs-20170630t121915694z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-06-30 12:19:24
last_update2017-06-30 12:19:24
depth1
children5
last_payout2017-07-07 12:19:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length75
author_reputation490,718,345,731
root_title"Learn Basic Python Programming EP.2. - Variables & Inputs"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id6,753,512
net_rshares893,731,963
author_curate_reward""
vote details (1)
@patriot-thoughts ·
$0.04
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.
πŸ‘  
properties (23)
authorpatriot-thoughts
permlinkre-btcunchained-re-profitgenerator-learn-basic-python-programming-ep-2-variables-and-inputs-20170630t232321717z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-06-30 23:23:12
last_update2017-06-30 23:23:12
depth2
children4
last_payout2017-07-07 23:23:12
cashout_time1969-12-31 23:59:59
total_payout_value0.032 HBD
curator_payout_value0.009 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length138
author_reputation24,098,233,643
root_title"Learn Basic Python Programming EP.2. - Variables & Inputs"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id6,824,175
net_rshares5,102,205,642
author_curate_reward""
vote details (1)
@btcunchained ·
Hum I have studied both... I think if you were exposed to python first, you will feel python is easy.. Likewise with cpp ☺️
πŸ‘  
properties (23)
authorbtcunchained
permlinkre-patriot-thoughts-re-btcunchained-re-profitgenerator-learn-basic-python-programming-ep-2-variables-and-inputs-20170701t055152671z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-07-01 05:51:57
last_update2017-07-01 05:51:57
depth3
children3
last_payout2017-07-08 05:51:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length123
author_reputation490,718,345,731
root_title"Learn Basic Python Programming EP.2. - Variables & Inputs"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id6,853,182
net_rshares818,287,057
author_curate_reward""
vote details (1)
@ragequit ·
$0.20
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!
πŸ‘  
properties (23)
authorragequit
permlinkre-profitgenerator-learn-basic-python-programming-ep-2-variables-and-inputs-20170630t113534149z
categoryprogramming
json_metadata{"tags":["programming"],"links":["http://www.dabeaz.com/"],"app":"steemit/0.1"}
created2017-06-30 11:35:36
last_update2017-06-30 11:35:36
depth1
children6
last_payout2017-07-07 11:35:36
cashout_time1969-12-31 23:59:59
total_payout_value0.198 HBD
curator_payout_value0.003 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length553
author_reputation84,968,448,410
root_title"Learn Basic Python Programming EP.2. - Variables & Inputs"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id6,749,542
net_rshares23,332,553,163
author_curate_reward""
vote details (1)
@profitgenerator ·
$0.20
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.
πŸ‘  
properties (23)
authorprofitgenerator
permlinkre-ragequit-re-profitgenerator-learn-basic-python-programming-ep-2-variables-and-inputs-20170630t113745900z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-06-30 11:37:54
last_update2017-06-30 11:37:54
depth2
children3
last_payout2017-07-07 11:37:54
cashout_time1969-12-31 23:59:59
total_payout_value0.196 HBD
curator_payout_value0.007 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length133
author_reputation68,549,319,463,075
root_title"Learn Basic Python Programming EP.2. - Variables & Inputs"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id6,749,736
net_rshares23,332,553,163
author_curate_reward""
vote details (1)
@kskarthik ·
Nice tut! 

`raw_input()`is replaced with `input()` in py3, right?
πŸ‘  
properties (23)
authorkskarthik
permlinkre-profitgenerator-re-ragequit-re-profitgenerator-learn-basic-python-programming-ep-2-variables-and-inputs-20170630t114326341z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-06-30 11:43:30
last_update2017-06-30 11:43:30
depth3
children2
last_payout2017-07-07 11:43:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length66
author_reputation220,092,999,236
root_title"Learn Basic Python Programming EP.2. - Variables & Inputs"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id6,750,255
net_rshares581,745,615
author_curate_reward""
vote details (1)
@profitgenerator ·
Actually I will transition to python 3, no need to teach people depreciated stuff!
properties (22)
authorprofitgenerator
permlinkre-ragequit-re-profitgenerator-learn-basic-python-programming-ep-2-variables-and-inputs-20170630t114238700z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-06-30 11:42:54
last_update2017-06-30 11:42:54
depth2
children1
last_payout2017-07-07 11:42:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length82
author_reputation68,549,319,463,075
root_title"Learn Basic Python Programming EP.2. - Variables & Inputs"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id6,750,193
net_rshares0
@ragequit ·
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!
properties (22)
authorragequit
permlinkre-profitgenerator-re-ragequit-re-profitgenerator-learn-basic-python-programming-ep-2-variables-and-inputs-20170630t114741748z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-06-30 11:47:42
last_update2017-06-30 11:47:42
depth3
children0
last_payout2017-07-07 11:47:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length176
author_reputation84,968,448,410
root_title"Learn Basic Python Programming EP.2. - Variables & Inputs"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id6,750,617
net_rshares0