Basic Concepts in Python 3

Python is a high-level programming language, defined as “general purpose”. This means it is intended for use in a wide variety of applications. It was first released in 1991, created by Guido van Rossum, with a vision of code readability, and since then it has been a favorite among programmers across the globe. Its plentiful use of whitespace makes the code clear for anyone to read, adding to its versatility. In this article I will highlight some of the basic operations that can be carried out in Python and provide a clear guide on how to get started with the language.

In order to start programming, you will first need to install python, along with a code editor, also known as an IDE (Integrated Development Environment). This is what you will actually use to type out your code and run it to make things happen. 

Download Pythondepending on what device you are on Windows, Mac or other platforms. 

Download code editor such as bracketsor use an online Python compiler.Once you have downloaded these you are ready to start coding!

The first operations you will carry out will be using the print function. This does exactly what you might think, it prints something to the console. The console is the area of the IDE that the code you write prints to, and where some user interaction can also occur if input is required. A favorite first program of beginners is the classic “Hello World” phrase. In order to print this simply type the following:

print(“Hello World”)

This will print the phrase inside the quotation marks to the console. The two important things to grasp at this stage are the necessary parentheses () surrounding the text, as well as the quotation marks themselves, as they denote that the type of data you would like to print is what is known as a string. 

A variable is essentially a placeholder that can store different types of data to be used in your code. A basic variable could be the name of a friend:

Strings are the first data type that you will encounter in Python. Strings are usually words or phrases contained within quotation marks however numbers can also be denoted as strings this way. If you want to print a number you may omit the quotation marks, but the way that numbers and strings interact can get complicated and will be discussed in a later part of this article.

Booleans are another data type, and these are simply True or False statements. These can be useful in more advanced operations that are out with the scope of this beginner’s guide.

Type conversion is a concept that involves converting one data type into another. For example, the “float” data type is used for certain mathematical functions and is used for numbers with a fractional value. This is in contrast to the “integer” data type, which refers to whole numbers. This is all to do with the way that Python uses memory, and floats are essentially used for numbers that require more memory than integers. Therefor we can convert numbers to floats, that can then be used in mathematical operations. 

For example, if we type the variables number_1 and number_2 equal to “5” and “10” respectively, and then try to add them together, Python will automatically combine them as strings:

Now, if we wanted to add them together as numbers, we could just define the variables as the number data type, but for the sake of explanation we will convert them to floats. To do this, we simply prefix the numbers in the print function by the word float():

The “+” function is one of the basic numerical operations that can be carried out in using Python. The other main ones are “*” for multiplication, “-” for subtraction and “/” for division. There are other mathematical operators and functions, but they will not be the focus of this article.

These operators can be shortened however, as the next example will show, using simple numbers for our calculation. 

If we want to use any mathematical operators on an already defined variable, we can use what are called “inplace” operators to condense the code. In our example we are using the variable number_1 and we give it a value of 5 to start with. In order to increase its value, in this case by 6, we could type out: number_1 = 5 + 6, but that would involve essentially reassigning the variable, as well as typing out more unnecessary code. That is where inplace operators come in handy, and they can be used to add to, subtract from, multiply and divide variables quickly and easily. This done by using the operators +=, -=, *= and /+ respectively. This becomes very useful when writing many lines of code using many different variables and functions. There are other inplace operators that can be used, but the ones discussed here are useful enough for Python beginners. 

The final element of this article involves simple input functions, to allow the user to have a direct impact on the code they have written while it runs. This example involves something called concatenation, which is the combination of two separate pieces of data to create one single piece, usually a string. In this example the two different parts that we need to concatenate are “your name is” and “name”. This is achieved using the simple + operator inside the print parentheses. 

The input operator is used to request the user’s input into the console. A prompt can then be included within this as a string, in this case asking the user to enter their name. This will then appear in the console, and when the user enters their name, the program moves onto the next line of code which in our case prints the user’s name. 

Congratulations! You have now completed some basic programming in Python and successfully ran your own program with user input. This is the first step toward creating some really great programs and having a solid knowledge of the fundamentals will make it much easier in the long run to get to grips with the more complex concepts and functions.

(Visited 9 times, 1 visits today)

Leave A Comment

Your email address will not be published. Required fields are marked *