Basic programming course: Lesson #2 - Variables and Types of Data.
4 comments
Hello Steemians, welcome to my post, and stay safe as you read through this post.
What are variables and what are they used for?
In the field of programming and mathematics, I will tell you that variables are very important in programming, variables store data that change as the program executes, whereas in mathematics variables means an unknown.
Having said that in summary, variables are symbolic containers or names that are used to store values or data in programming specifically. Variables can hold various types of information like text, numbers, or complex data structures. The use of variables is to provide a way for data to be referred to or identified so it can easily be retrieved, manipulated, or updated without needing to repeatedly reference the data again.
Still discussing variables as we earlier said in programming, variables store data that can change as the program executes. To this, the use cases of variables in programming involve the storing of user input, and immediate calculations resulting from functions. Examples of variables in Python are shown below.
age = 28 # Variable holding the number 28
name = "Josepha" # Variable holding the string "Josepha"
In summary, with variables it is easier to work with data flexibly and dynamically.
Assign a type of data to the following variables and explain why: email, phone, working hours, price_steem, and age.
Below is an assignment of data types to each of the following variables given in the question with a brief explanation for the reason why.
email (string)
Type: String
(Text)
Why: This is because an email address consists of a combination of letters, symbols, and numbers which makes an email a sequence of characters. For example, my email is [email protected]
.
Phone (Integer or String)
Type: Integer
(when operation that has to do with numeric is required), String
(for common choice).
Why: Phone numbers are numerical, which also involves the use of special characters like +, -
. It is treated as strings to protect it from formatting.
Working Hours (Float or Integer)
Type: Float
or Integer
Why: This is because working hours is typically a representation of the number of hours a person has worked, which is normally measured in whole numbers (Integer). In a situation where partial hours like 8.5 are needed Float
is best used.
Price Steem (Float)
Type: Decimal number (float
)
Why: This is because Steem is like every other cryptocurrency and prices are usually in decimal points like for example, $0.16. Also, since the price of STEEM fluctuates like other cryptos it includes fractions of units of currency.
Age (Integer)
Type: Whole number (integer
)
Why: This is because age is expressed as a whole number of years, which means integer
is perfectly suitable for assigning age.
Explain how the following code works:
Algorithm names
Define name, last_name as Character;
Print "Enter your name:";
Read name;
Print "Enter your last name:";
Read last_name;
Print "Hello " name " " last_name ", welcome";
EndAlgorithm
Since the algorithm is written in pseudo-code the steps for how the code will work are shared below.
Steps : 1 Define Name
Two variables: name
and last name
are defined by the algorithm both are data type Character
. This simply implies that both are intended to hold text.
Steps: 2 Print "Enter your name
The algorithm prints the message as; "Enter your name:"
After the algorithm has printed the message, and waited for the user to input their first the input will then be stored in the variables name
.
Step: 3 Read name
This is the line of code that gives the user access to input their information.
Step: 4 Print "Enter your last_name"
The algorithm prints this message after the first message giving the user access to enter the last name.
`"Enter your last name:"`
User will then be prompted to enter their last name, and the input will be stored in the variable last name
.
Step: 5 Read name
This is the line of code that gives the user access to input their information.
Step: 6 Print "Hello " name " " last_name", welcome"
At this point, the algorithm prints a greeting using both the first name and the last name variables.
`"Hello " name " " last_name ", welcome"`
This will display something like:
`Hello Josepha, welcome`
Step: 7 EndAlgorithm
At this point EndAlgorithm
tells you that your program has concluded (end).
Develop a pseudo-code to calculate the value in USD of X STEEM
At this point, since I had already installed the PSeint application on my system via the previous lesson, I went straight and opened the application and developed the pseudo-code shown below.
Algorithm USDT_of_Steem
//Create variables
Define price_steem, amount_steem, total as Real;
//Request data
Print "Enter the current price of STEEM in USD:";
Read price_steem;
Print "Enter the amount of STEEM in your wallet";
Read amount_steem;
//Calculate the total value
total = price_steem * amount_steem;
// Show the result
Print "The total value of your STEEM in USD is: " total;
EndAlgorithm'
Output
I am inviting: @pelon53, @dove11, @simonnwigwe, and @ruthjoe
Cc:-
@alejos7ven
#devjr-s20w2 #learnwithsteem #devwithseven #steemexclusive #nigeria #club5050
Comments