python

PYTHON TUTORIAL 1 – VARIABLES AND PRINT STATEMENTS

 

    In this beginner python tutorial we will begin some basic syntax. We will look at what variables are, what they do and print statements that display an output to the console. The code snippets are being typed in IDLE and run directly in the python shell. Alternatively you can use a text editor of your choice and run the code through the command prompt instead. If you need help with choosing and downloading a text editor and downloading python refer to this tutorial here and if you need a basic rundown of the command prompts functions and how to use them refer to this tutorial here.

VARIABLES

    A variable is an object in python that holds a value that you assign to it. Variables are useful because they allow you to utilize a particular value multiple times in your code by referring to the variable instead of having to type out the value over and over. Variables are also mutable which means they can be changed. We will go over what kinds of data types we can assign to a variable in the next tutorial.

    In the example above we assign the string “HELLO WORLD” to a variable called HELLO. When we ask python what is being stored in the variable HELLO it would output the string “HELLO WORLD” which is what happens on line two of the example.

VARIABLE REASSIGNMENT

     The value of a variable can be reassigned at any time you simply have to type the variable’s name again and after the equals sign type in a new value to assign to the variable.

    In the above example we first assign the value “HELLO WORLD” to the variable HELLO. On the next line we change the value of HELLO to “HI WORLD”. We then print the value of HELLO to the console.

MORE REASSIGNMENTS

You can also assign one variable to be the value of the other variable.

python code

    In the above example we make a new variable white and assign it the value of 5. We then make a new variable called black and assign it the value of white. Keep in mind the variable is always on the left side of the equals sign and the value of the variable is always on the right side. When we print the variable black you can see that the value of white was printed out because the value of black now equals the value of white.

 

    In the above example we create a new variable called black_and_white and we set it to the value of adding the values of black and white together then we print it out. The value of white was 5 and the value of black was set to the variable white giving it the same value so when added together the new value assigned to the new variable was 10. You can use math operations in python to do a variety of different things not just with numbers but with variables and other objects as well.

PRINT STATEMENTS

    The print statement simply prints whatever is in the parentheses following the statement. The print statement can also print more than one thing by taking in multiple arguments.

python code

    In the above example our print statement takes in three arguments instead of one and it prints each one on the same line. If we wanted to print those statements each on their own line each one would need its own print statement as seen in the picture below.

python code

python code

 

This has been a very basic introduction to python syntax. In the next tutorial we will be covering different data types such as strings, integers, and float numbers.