image
image
image

Chapter 2: The Basics of the Python Code

image

There are many different types of codes that you are able to write inside of Python. Some are going to be pretty simple to write and will just include a small line and others will be much longer, taking up long lines of code in order to get the point across and to tell the compiler what you would like to see happen. But no matter what kind of code you are writing, whether it is a short code or a much longer code, there are several parts that are similar in all the codes. Learning how all of these work and putting them together can help you to really write out the codes that you want and will be useful as you write some of the more complex and difficult ones down the line.

The Python keywords

Like many of the other programming languages that you will use, Python has some keywords that are specific just to it and that you can’t use in other parts of the code. These keywords give commands over to the compiler so that it knows how to react to the codes that you are writing. This is why they are reserved; if you start to use them in other parts of the code, you are going to confuse the compiler and could have some issues with the code working the way that you would like.

Naming the identifiers

While you are working inside of Python, or any other programming language, there are many identifiers that you will work with. These go by many different names including classes, entities, functions, and variables. When you try to name one of these identifiers, you will be able to use the same information and rules for each of them. Some of the rules that you have to keep track of when working in python include:

You are able to use a wide range of naming options including upper case and lower case letters as well as numbers and the underscore symbol. You can use any combination of these that you would like, you just need to make sure that you don’t add in spaces into the name if you have more than one word for it.

Your identifiers should not start with a number. You can have a number elsewhere in the code, it just can’t be the first character in the name.

The identifier is not able to contain one of the keywords. This is just going to confuse the compiler and can get issues with errors in the program.

For the most part, you should have plenty of names that you can use for naming your identifiers so you shouldn’t have too many issues with these rules. If you happen to go against one of the rules that we listed above, you will see that the compiler will show a syntax error and it will close up on you.

There are a few other things that you should consider when naming the identifiers as well, even though these wouldn’t effect the code in a way to give you an error, they are still important to make things easier. For example, you want to make sure that the name is easy to read because you want to allow others to read through the code without having trouble understanding what you are trying to do. Picking out a name that is descriptive can help as well because it will allow you to remember what you wanted the identifier to do in the first place.

Flow of control in Python

The flow of control is important inside of a coding language because it helps you to determine how you should write out the code that you want to have done. For Python, it is going to read the codes from the top to bottom, in a similar way that you would read a book at home. And to ensure that you are going to keep the code running as smoothly as possible, you should write out the different parts like you would a grocery list. Some people like to write out all the commands right next to each other and make it a mess, but this is not only hard to read, but also makes the code cause more errors. Write out each of the parts of the code on a separate line to help the flow of control go as smoothly as possible and to ensure that others are able to read it properly.

Statements

Statements can be really useful inside of your code because they help you when writing out your code. These are going to be the strings of code that you write out to tell the compiler what you would like to have done when the code is executed. As long as they are set up in a way that the compiler is able to understand, you are going to get a good code that will work well on the computer. The statement can be short, just having a few lines in them or they can be really long and contain a full block of code.

Comments

There are times when you will want to write out comments inside of your code. These are helpful because they can help you to name the code or leave other information into the code so that other programmers are able to reach it and understand what is going on inside of the code. You will find that the compiler will look for the comment sign (which is the # sign) and then it will skip over the comment and move on to the next line of the code. This means that the comments won’t affect what is executed inside the code, it will just be there for others to use.

You are able to add in as many comments as you would like to your code, but try to keep it to a minimum and only add in the comments that are really needed. This helps to keep the code easier to read and ensures that the compiler is able to just read through the parts that you need, rather than getting confused by all of the comments that are there.

Functions in Python

When we are talking about a function inside of our coding language, we are talking about a block of code that you can reuse and that will be used in order to perform just one action. Functions are going to help you to reuse your code much better than before and will provide more efficiency all around the code. There are many functions that are already inside of the Python code, but you get the benefit of creating some of your own if you would like to add these in yourself.

Inside your code, you are going to want to define a function at some point. There are a few rules that you have to remember when it comes to defining the function to make sure that it works out the right way for you. Some of the rules that you will need to follow include:

Once you have defined the function and finalized the function, you will need to execute it within the code. You can simply call it from the Python prompt or from another function. A good example of how you would call up your function can be found in the example below:

#!/usr/bin/python

# Function definition is here

def printme(str):

"This prints a passed string into this function"

print str

return;

# Now you can call print function

print ("I'm first call to user defined function!")

print ("Again second call to the same function")

When you place this into your code, you will get the two statements that are inside the code to come up in a message. This is a simple example of calling up the function and you are able to go through and decide on which statements are going to fit into the code and how they are going to execute later on.

Variables

Variables are basically the locations in the memory of our computer that are reserved in order to store some values. Any time that you decide to create a new variable, you are going to reserve a bit of space inside of your memory. Depending on the data type that you place into the variable, your interpreter will be able to decide where it will be stored for you to find later. This makes it easier for you to assign all the different types of data to your variables including characters, decimals, and integers.

It is your job to assign the right values to the variables so that they are going to work properly inside of the code. You can basically give the variable any value that you would like, but it is best to pick the ones that will work the best in your code and to call them up properly before you try to use them.

When you want to assign a new value over to one of your variables, you would need to use the equal sign (=), to show which value goes to which variable to make things easier. Here is a good example of how you would get this done inside of Python:

#!/usr/bin/python

counter=100  # An integer assignment

miles=1000.0  # A floating point

name="John"  # A string

print counter

print miles

print name

In this example, you would get the results that went with the variables that you assigned to each value. For example, the counter would give you the result of 100, the miles would give you the result of 1000, and the name would give you the result of John.

Understanding how a few of these topics work will help you to get our code to work out the proper way. Make sure to keep some of these tips in mind when you start to write out your codes so that you are able to get the compiler to read through and execute the code that you would like.