I LEARN PROGRAMMING - SWAP VARIABLES, INPUT AND OUTPUT EP#2

in #programming6 years ago (edited)

program-01.jpg

imgsource

Hello friends, This is a continuation of my programming series, in which, I teach myself how to code. I believe if I have a formal documentation about my progress, I will be less complacent, I will learn and also share my knowledge and encouragement to potential programmers out there.

SWAPING VARIABLES

In the previous episode, we talked briefly about variables, variables is a memory allocation in the computer that possesses a name, a value, and a type.

The value stored in the variable depends on the data type of the variable. The common data types that we'll be talking about is

The integer(int) - whole numbers

Float - real numbers with fractions and decimal points

Strings - letters and characters.

To swap two different variables, first, a value is assigned to the variables.
1.jpg

Then a display print command is used to show the values of X and Y before swapping, the print command follows the python print syntax and semantics.
2.jpg

The swapping algorithm is like, let's say, you have two containers containing x liquid and y liquid, you want to swap both contents without mixing, the solution would be to find a temporary container and pour x in it. then pour the y liquid in x. then finally you pour the temp contents of x into y.
3.jpg

A print command is entered to display the swapped values
4.jpg

After the whole code is executed, this is what you get.
5.jpg

After the successful execution of the code, I proceeded to learn more about manipulating strings and assigning a value to string variables.

Assigning a string of characters to variables, string and string2
6.jpg

Adding two strings together is as easy as adding both variables together but I added with a " " space in between them. even space is a string value. All string values are entered in quotes(double or single quotes)
7.jpg

Continuing with the additions, I choose to add a raw string format to the stored variables and it works just as much. this is a code that adds a greeting word to the string variables and displays it. the "\n" is a command that is added to make the display go to the next line after execution so it more neat format.
8.jpg

This time I choose to multiply the string values in the variable with the multiplication sign and it works but subtraction and division signs don't work in string manipulation.
9.jpg

It is also possible to get the length of a text with the len function. this is already stored in commands in the python software that can execute and return the integer number of your string lengths, I wanted to find the length of the greet string variable and then stored it in the variable (length_of_text)
10.jpg

But I discovered that it also counted the spaces in between so I did a quick search in the StackOverflow website and I found someone with an answered and same questions as me.
I stored the values into "no_spaces_length" and the code also involves using an inbuilt function called (.count), the syntax involves putting whatever strings you want to count in front of the function, and the original string is placed behind the function. for instance, greet.count(' ') is used to count the spaces found in the greet string variable. so I subtracted the counted spaces from the total len function value.
Then the value of "no_spaces_length is printed"
11.jpg

The execution of all the code is displayed as followed
12.jpg

Afterwards I practiced a little bit about taking input from the user, I wrote a code to accept a name from a user and store it in the name variable, and later displaying it for the user to see.
13.jpg

I also wrote another one for accepting height from the user, storing the value and displaying it later. It follows the same syntax as the last one but this time, because we are accepting integer values, I defined the input by placing it inside an integer closed bracket.
14.jpg

the execution of the code is displayed below, I entered the required values at the displayed prompt of the program
15.jpg

banner.png
That was all my practice for today, I want to encourage anyone learning to programme to start blogging about it, because it will help you a lot, as I was typing and explaining each line of code I began to gain a deeper understanding of what I was typing, it's that kind of practice that helps algorithmic thinking. also blogging daily gives you that push to learn something new everyday, thanks for reading :-)

Episiodes: 1

Resources
EDX.ORG(MIT website) - INTRODUCTION TO COMPUTER SCIENCE USING PYTHON
LEARN PYTHON THE HARD WAY
OFFICIAL GUIDE FROM PYTHON WEBSITE
TUTORIAL POINT


road-1668916_960_720.jpg
DQmP3mL7sAPk3qBvCARHZCK8RzReNdkgz6hYBxmrxDC3u32_1680x8400.jpg
imgsource

Sort:  

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by abmakko from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.

Someone is really enjoying this.. I don't have eyes for stuff like this but in this era we are in presently one should know the basics at least. Keep it up

Coin Marketplace

STEEM 0.19
TRX 0.16
JST 0.030
BTC 66739.21
ETH 2594.70
USDT 1.00
SBD 2.70