Learning the Basics of Python Programming | CMD TO
Now it is time to get to know a bit more about Python programming and how
you can make it work for you. You will need to learn a bit more about the
different keywords and the variables that come with Python so you are able to
write the words that you want and make the program perform in a certain way.
Let’s take a look at some of these basics of Python programming so you can
get started with your new code right away.
Keywords
When you are working on a new computer coding program, you are going to
notice that each computer language will have certain keywords. These are the
words that are meant for a specific command or purpose in the language and
you should try to avoid using them anywhere else. If you do use these words in
other parts of your code, you may end up with an error alert or the program not
working properly. The keywords that are reserved for Python include:
And
Pass
Or not
Nonlocal
None
Lambda
Is
In import
If
Global
From
For
Finally
False
Except
Else
Elif
Del
Def
Continue
Class
Break
Assert
As
Yield
With
While
Try
True
Return
Raise
Identifier Names
When you are creating a new program in Python, you are going to work on
creating quite a few entities, a combination of functions, classes, and variables.
All o these will be given a name that is also known as an identifier. There are a
few rules that you need to follow when forming an identifier in Python
including:
It should contain letters, either uppercase or lowercase or a combination
of the two, numbers, and the underscore. You should not see any spaces
inside.
The identifier can’t start with a number
The identifier can’t be a keyword and it shouldn’t include one of the
keywords inside.
If you break one of these rules, the program will close on you and will show a
syntax error. In addition, you need to work on making identifiers that are legible
to the human eye. While the identifier may make sense to the computer and get
through without causing issues on the computer, a human is the one who will
read through the code to use it themselves. If the human eye doesn’t understand
what you are writing in a certain place, you could run into some issues. Some
of the rules that you should follow when creating an identifier that will be
readable to the human eye include:
The identifier should be descriptive—you should pick out name that is
going to describe what is inside the variable or will describe what it
does.
You should be careful with using abbreviations that aren’t necessary
because these always make things that are difficult.
While there are a lot of ways that you can write out your code, you should be
careful and stick with one rule throughout. For example, both MyBestFriend
and mybestfriend work in the coding world, but pick one that you like and do it
the same each time that you work in the program to avoid confusion. You can
also add in underscores into this or numbers, just be careful that you keep
things consistent.
Flow of Control
When working on the Python language, you are going to write out the
statements in a list format, just like you would when writing out a shopping list.
The computer will start with the first instruction before working through each of
them in the order that you make them show up on the list. So you will need to
write out the controls that you want just like you would for your grocery
shopping list to make sure that the computer is reading it properly. The
computer will only stop reading through this list once it has done the final
instruction to completion. This is known as the flow of control.
This is an important way to get started. You want to make sure that your flow
of control is even and smooth for the computer to read. This will make it easier
to get the program to do what you would like without as many issues and
ensures that the computer program doesn’t get stuck, cause issues, or have
something else go wrong.
Semi-colons and Indentation
When you look at some of the other computer languages, you will notice that
there are a lot of curly brackets used to arrange the different blocks of code or
to begin and end the statements. This helps you to remember to indent the code
blocks in these languages to make the code easier to read, although the
computer will be able to read the different codes without the indentations just
fine.
This type of coding can make it really difficult to read. You will see a lot of
unnecessary information that is required for the computer to read the code, but
can make it hard on the human eye to read this. Python uses a different way of
doing this, mostly to help make it easier on the human eye to read what you
have. You are going to need to ident the code for this to work. An example of
this is:
# this function definition begins a new block
def add_numbers (a, b):
c= a + b
# as is this one
return c
# this function definition begins a new block
if it is Saturday
print (It’s Tuesday!”
# and this one is outside the block
print (“Print this no matter what.”)
In addition, there are a lot of languages that will use a semicolon to tell when
an instruction ends. With Python though, you will use line ends to tell the
computer when an instruction will end. You will be able to use a semi-colon if
you have a few instructions that are on the same line, but this is often
considered bad form within the language.
Letter Case
Most computer languages will treat uppercase and lowercase letters the same,
but Python is one of the only ones that will be case sensitive. This means that
the lower case and upper case letters will be treated differently in the system.
Keep in mind as well that all the reserved words will use lower case except for
None, False, and true.
These basics are going to make it easier to get started on the Python
programming. You need to take a bit of time to go through the program in order
to get familiar with it. You aren’t going to need to become an expert, but getting
familiar with some of the text interpreter and some of the other parts of the
program can make it easier to use and you can learn how the different buttons
will work even before you get started. Try out a few of the examples above
first to help you get started.
Python works to keep things as basic as possible because it understands that
most of its users are going to be beginners or those who are tired of other
complex languages. As you can see here, and in the following chapters, there
are simple commands that you will be able to put forward in order to get the
program to work a specific way. Study these and you can make a great program
without quite as much work.
No comments:
Post a Comment