Robot Thoughts
Published in

Robot Thoughts

Learn Practical Programming: Step 1+

Photo by David Clode on Unsplash

Now that we got the basics of variables, operations, input, and output, it’s time to test them on the computer. I suggest creating a new folder for the files of this course.

Create your file (you can name it step1.py) and open it with VS Code or any IDE of your choice.

I'll leave a quick cheat sheet on how to implement what you learned in step 1 in python.Operations:
sum: a + b
subtraction: a - b
division: a / b
multiplication: a * b
concatenation: str_a + str_binput: input("your message here")output: print("your message here")comments: # we use the # symbol to write comments

CASTING

Before I leave you some examples, you should know that the computer (and python) isn’t smart enough to know what type of variable the user is supposed to type when using the input() function, so we’ll need to tell the computer what type of variable the program should read. This is called casting.

It’s quite easy to do, all we have to do (in python) is to surround the input() with a parenthesis preceded with the type of the variable we want to receive. Here is an example of casting input when we want to receive a number (when we want to receive text we just leave it as default, without casting).

When we want to receive an Int (whole number):
int(input(_yourMessage_))

You should also know that we can also use casting with variables instead of the input function. Here’s an example of how we could turn a number into text.

a = str(myVariable)

EXAMPLES

Now, we’re ready to go over some examples. Please read them carefully, it’s important that you understand every line of code before continuing with the next lesson.

EXAMPLE: a program to ask for 2 numbers and return the sum in python.

Notice how there’s a cast to transform the input (which is a text/string as default) into a number variable, this is to be able to do the sum later instead of concatenating/joining the numbers as if they were text. And then, in line 5 there’s a cast to transform the variable c (which holds the result of the sum) into a string (text), this is so we can concatenate/join the number with the text “The result is: ”.

EXAMPLE: a program to ask for the user’s name and salute him/her, in python.

TEST

Write a program that asks for the user’s name, his/her age (in years), and a favorite number, then multiply the age by the favorite number, save it as a magic number and then print a message with such number. Like this:

Hi Petyr, your magic number is 24.

Please try to solve it yourself before reading my solution (and you should also check that the magic number which is being printed is the correct multiplication of both numbers given by the user).

Here’s a link to my solution.

I hope you liked this post and found it useful, please feel free to leave any questions in the comments section. Don’t forget to follow us here at Robot Thoughts. Until the next step!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
SSAM

SSAM

I’m a computer science student and I write about programming, roleplaying games, sci-fi, movies, books, finances, and anything that comes into my mind.