PythonIsCool – Chapter 1

Chapter 1

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 what…


This content originally appeared on DEV Community and was authored by Medea

Chapter 1

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!"

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?")

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

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!


This content originally appeared on DEV Community and was authored by Medea


Print Share Comment Cite Upload Translate Updates
APA

Medea | Sciencx (2022-04-24T16:27:41+00:00) PythonIsCool – Chapter 1. Retrieved from https://www.scien.cx/2022/04/24/pythoniscool-chapter-1/

MLA
" » PythonIsCool – Chapter 1." Medea | Sciencx - Sunday April 24, 2022, https://www.scien.cx/2022/04/24/pythoniscool-chapter-1/
HARVARD
Medea | Sciencx Sunday April 24, 2022 » PythonIsCool – Chapter 1., viewed ,<https://www.scien.cx/2022/04/24/pythoniscool-chapter-1/>
VANCOUVER
Medea | Sciencx - » PythonIsCool – Chapter 1. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/24/pythoniscool-chapter-1/
CHICAGO
" » PythonIsCool – Chapter 1." Medea | Sciencx - Accessed . https://www.scien.cx/2022/04/24/pythoniscool-chapter-1/
IEEE
" » PythonIsCool – Chapter 1." Medea | Sciencx [Online]. Available: https://www.scien.cx/2022/04/24/pythoniscool-chapter-1/. [Accessed: ]
rf:citation
» PythonIsCool – Chapter 1 | Medea | Sciencx | https://www.scien.cx/2022/04/24/pythoniscool-chapter-1/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.