Welcome to an introductory tutorial of programming in Python.
If this is your first time touching any programming language, this tutorial will be essential for the future tutorials. If this is not your first programming language, some material may seem similar to your previous experiences in programming.
Python unlike other popular programming languages is an interpreted language (depending on your implementation), meaning that you don't generally need to compile and run Python unlike with Java and C++. When you run a Python file, you get an output without needing to do anything else.
I don't like to learn theory before implementation, which is the case with many other students, which is why I will show and then explain what is going on from now on.
print() is a Python function. Functions are executed by computers to do something, in this case, the print() function outputted the string parameter to the terminal
Calling the print() function tells the computer to execute it. Try editing the parameter above to anything you want like, "Hello , insert name".
One of the reasons you can't write
is because of something called Syntax. Just like in English grammar, you c'ant p.ut pe,riods and c!om,mas a,n;yWheR.e you like, programming languages have strict rules of how you can use them so that they perform exactly how you'd expect.
If you're the next generation of coders, you need to know one thing from an experienced programmer; Comment everything. There's basically not enough commenting you can do in your code.
Essentially what commenting in programming is, is what you'd expect; comments in your code, indicating what the function or class does and what parameters it expects and what it returns. Comments also sometimes indicate what a process is doing within a function. Comments can help others debug your code as well.
This is an example of comments. Comments start with a
Each line that you want to comment (sometimes you may want to comment out code for debugging) needs to start with a hashtag.
Want to store something? Something like your name or a long number that you can't keep in your head for too long? Well programming can help with that. Just create a variable like this:
Congrats! You just created your first variable in Python! Now instead of memorizing that variable a is "This is a", Python stores it to be used anytime.
In a more practical sense, storing variables becomes more useful when doing something like this:
Instead of typing the numbers 9 and 10 another time to find out their sum, you can reference to their variables to add them up to be stored in another variable, thirdNumber.
Try changing the variable values and variable names! Note: writingLike thisIs called camel case. This is common practice when making variable and function names because it is easier to read.
You can concatenate string variables and manipulate number with variables. Let's see that in action:
Be honest, this is pretty boring right?
Just writing some code and getting a plain output, right? Well not anymore with inputs. Set a variable to an input to store the input, like this:
will store the input.
Now you can add inputs. Fill inputs in the program below to see the funny sentences you can make:
Concatenation two strings will produce another string no matter of the content of the string.
You can also turn variables c and d into variables equivalent to a and b like this:
Try it out yourself:
Need to keep a secret? Do it with Python Equality! Set a password and only print out your secret message if the password matches with the input.
and indicates that two boolean expressions need to be true for the output to be true, otherwise it is false.
not reverses the boolean expression
or indicates that either boolean expression needs to be true for the output to be true as well.
<,>,>=,<= can also be used for number operations.
This is a for loop. It recurses through the list or a set of numbers. Another for loop is:
A while loop allows you to go on a loop forever as long as a certain condition is met. In this case the variable number has to be less than 10 in order to loop. This means numbers up to 9 should only be printed.
This is concluding the Python introductory tutorial. More tutorials are being made now.