Python: LET'S GOOO!
In many programming languages, some special words tell the compiler where to start running, or executing, your code.
For example, C and C++ have a special function called `main()`. When you write a program in C or C++, it will know to start running the code you put in `main()` first. You might get an error if you try to put a line of code that isn't inside of `main()` or some other function. In Python, it is a bit different. This quick lesson will focus on how Python determines what code to run and when to do it.
First, let's look at this line of Python code:
When you run this line, it will print "I am executed first" because it is the first line of code the compiler finds. It doesn't need to be inside a function, Python will still run it.
Now look at this:
You can see that it will print the first line and then move to the next. So in Python, it just moves down line by line.
Writing a few lines of code like that is fine, but when your Python scripts get longer, it is better to keep things organized in functions. Let's put our `print()` line in a function and see what happens.
If you run that, you won't see anything. The `print()` line is inside a function called `print_steamrocket`. Python won't execute a function unless it is called somewhere in your script, so it skipped over it when it saw that line. To see that code, we would need to call the function.
We could do this:
Now when we run it, the first line of code it can run is our `print_steamrocket()` line.
This works just fine. You can add more functions and call them in any order you like. Doing things this way lets you keep things organized and easy to understand.
Are you up for a challenge? Play around with the code to see if you can:
change the name of the function
add another function
add a print() line between your functions and see what happens
For example, C and C++ have a special function called `main()`. When you write a program in C or C++, it will know to start running the code you put in `main()` first. You might get an error if you try to put a line of code that isn't inside of `main()` or some other function. In Python, it is a bit different. This quick lesson will focus on how Python determines what code to run and when to do it.
First, let's look at this line of Python code:
When you run this line, it will print "I am executed first" because it is the first line of code the compiler finds. It doesn't need to be inside a function, Python will still run it.
Now look at this:
You can see that it will print the first line and then move to the next. So in Python, it just moves down line by line.
Writing a few lines of code like that is fine, but when your Python scripts get longer, it is better to keep things organized in functions. Let's put our `print()` line in a function and see what happens.
If you run that, you won't see anything. The `print()` line is inside a function called `print_steamrocket`. Python won't execute a function unless it is called somewhere in your script, so it skipped over it when it saw that line. To see that code, we would need to call the function.
We could do this:
Now when we run it, the first line of code it can run is our `print_steamrocket()` line.
This works just fine. You can add more functions and call them in any order you like. Doing things this way lets you keep things organized and easy to understand.
Are you up for a challenge? Play around with the code to see if you can:
change the name of the function
add another function
add a print() line between your functions and see what happens