DEV Community

Medea
Medea

Posted on • Edited on

PythonIsCool - Chapter 1

Chapter 1 - Print, Input and Variables

Table of Contents

Incorrect Code
Syntax Highlighting
The Print Function
The Input Function
Variables


Incorrect code

To start writing your first line of the code, click the first line of code. Now write whatever you want to write on the line and press run. Luckily for you, if you write a code which doesn't make sense, you will see an error like SyntaxError : invalid syntax in red in the console. There are other types of errors like the UnboundLocalError, but the most common error right now will the SyntaxError.


Syntax Highlighting

Syntax Highlighting is a feature of text editors that make it easier to read the code. So different code parts will have different colours.


The Print Function

The most basic function in Python is print(). It is a simple function that just writes whatever is in the brackets in the console. For instance, the way to say Hello World! in Python is print("Hello World!"). If you write that in code, the output would be Hello World!. Have a go at saying Hello World! to Python in your code. Try running your code now by pressing the run button. The output should look like the picture below.

Hello world

If it does, then well done! You have written your first line of proper Python code! If not, compare your code to print("Hello World!") and figure out what is wrong.


The Input Function

Input is another common function in Python. It is used to ask the person testing out your code a question. For example, if you want to ask, How are you?, you would write, 'input("How are you?")'. The picture in the next page shows how the console would look like after you ran the code.

input

In the picture above, the code hasn't finished running, as it is still waiting for the user's response to the input.
After you answer the input code, then the line of code is officially finished.

Variables

Variables are words that have values assigned to them. The values could be numbers, lists, strings, etc. An example of setting a variable is in the code below:

hello = "Hello World!"
Enter fullscreen mode Exit fullscreen mode

In the code's case, the variable name is hello, and the value is Hello World!. Now if you want to show Hello World! in the console, you can just set a variable called hello with the value of Hello World! like above, and then print(hello), bearing in mind that because hello is a variable, you don't put speech marks around it.

Now if you run the code, the result will be the same as the time you wrote the code print("Hello World!").

Another example of a variable is the code below:

ask = input("How old are you?")
Enter fullscreen mode Exit fullscreen mode

If you type up that code, the result will be the same as your first input statement.

As I said, variables can also be numbers, so if you want to assign the variable 'one' the value of the number 1, you will have to do the code below.

one = 1
Enter fullscreen mode Exit fullscreen mode

After you copy the code above, if you have copied it correctly, you shouldn't see any errors, and the console should have nothing on it. That is because you haven't called any functions which use the console to display anything.


Thanks for reading and I hope you learnt something from this!

Top comments (0)