Python: how to begin

in #python3 years ago

If you want to start programming, python is one of your best choices. It is easy, fun to learn, and supports a wide variety of API's (Application Program Interfaces), all of which are important to becoming a great programmer.

Start by downloading python and clicking the big yellow download button (you cannot miss it). If you are on windows, run the .exe, select your install options and click install. On Linux you probably already have it installed but to check, run whereis python3 in your terminal. On linux you will mainly be operating in the terminal so you need to have a good understanding of bash. I will make a blog post about that.

Next you will need to open up python. On windows search for IDLE in the search bar and on linux run python3 in your terminal. From here things will mostly be the same on both systems. You will see >>> at the bottom of the screen. That is the prompt and it is where you will type python statements. Type print("Hello world!") into python and press enter. Hello world! should appear back on your screen. If it did not, then you must have done something wrong like misspell print, forget a " or forget a bracket.

If it did work, congratulations! Now lets break down what you did: print is a function, which is indicated by the brackets and in between the brackets are the arguments, which you only provided one. Lastly you have the string, shown by the two double quotes surrounding it. You may put anything in a string* and the code will not be affected. The print function will output anything that you put in as arguments.

Now you need to make a variable. Type var = 11 into python and press enter. If you then type var or print(var) into python, it should return the value you set for var. You may use any* name for a variable. Notice how 11 was not surrounded in double quotes? That is because any number you type into python has its own separate class, int, which means integer. If you set a variable to "11" it will be different from 11 because one is a string and the other is an integer.

Next we will make a script. A script contains multiple python statements which are run consecutively, and can also contain code blocks that allow creating functions, if statements and loops. On windows, press Ctrl+N to make a new script and on linux, exit python with Ctrl+D, then open up your favourite text editor. I will use GNU Nano for this demonstration, so run nano myscript.py in the terminal and begin editing. We are just going to cover if statements for now.

Type the following code into the editor:

print("Welcome to python!")
print("I am printing multiple statements")
print("This script has finished executing")

Then on windows press Ctrl+Shift+S, select a place to save your file and type myfile.py into the box at the bottom and then press F5 to run it. python files typically end with .py or .pyw. on linux press Ctrl+S to save your file, then press Ctrl+X to exit and run python3 myfile.py in terminal to run it. The three lines shown in the strings will appear on the screen and on windows the prompt will be shown again, but on linux you will go back to terminal.

Next go back to the editor (close the prompt window on windows, or run nano myfile.py again on linux) and add this:

myint = 2

if myint == 2:
    print("My integer is 2!")
else:
    print("My integer is not 2")

Save the file with Ctrl+S and run the file with either F5 or Ctrl+X and python3 myfile.py. This time the first three strings will appear, followed by "My integer is 2!". Lets break that down. First we set myint to 2 and then we created what is called an if ... else block, because it has an if and else keyword. next to the if keyword we had the condition myint == 2. This asks whether myint is equal to 2, which it is. The == operator means is this equal and the colon allows you to create an indent, which is 4 spaces that precede a line of code telling it what statement it is apart of. the else keyword is the opposite of if, because it will trigger its block of code only if myint is anything but 2.

We are done for now! Come back soon for more python tutorials!

Sort:  
Loading...