Learn Python in 15 minutes

Bytes of Intelligence

Learn Python in 15 Minutes

In this tutorial, I will give you a brief introduction to Python 3 with some basic syntax, to get you started on your programming journey. Python is one of the most popular programming languages for all developers and programmers, and it is known as a “readable programming language” because it is fairly easy to learn and simple to code.

Free Online IDE

To even get started on coding, you have to install an IDE – or integrated development environment, which is simply a term for a text editor. For this short tutorial, I recommend using replit.com because this is an online text editor, which will allow you to experiment with python quickly, without the installation process – which is quite time consuming if you just want to learn the basic syntax. I love using Replit, mainly because I can use it straight from my browser, so it easy to dive in and code anything I want.

So type the URL: replit.com into your web browser, create a free account, and open a new Python 3 repl. In replit, we have a light coloured screen on the left and a dark coloured screen on the right. The left side is where you will write your program, where you write the Python itself. The right side is the console where you can get instant feedback from your code.

Inputs and Outputs

The best way to start learning Python is to perform an input and an output. In Python, we can use the PRINT function to output any text onto the screen.

print('I am learning to code')

In most programming languages, including Python, there is specific syntax that you need to use when communicating with the computer. So in the example above, after print, we use a pair of brackets and we also use a pair of single quotes wrapped around the text. This is to indicate that we only want to output the text inside the quote marks, and nothing else. In this case, the text “I am learning how to code!” is a string – which is a fundamental data type in Python.

Similarly, we can use the INPUT function to ask a user to input a value and store it for future use. I am going to make a variable called value (but I will explain what variables are shortly).

value = input(“Please enter any number: ”)

This allows a user to enter a number, and this will be stored into the variable “value” for future use. 

Variables

Variables are memory locations which store specific information or values in a program. We can name variables in a descriptive way so that later on, we always understand the purpose of certain data. A way to imagine this is that a variable is as a container, where we can store different items for future use. We can also write a name on the outside of the container so that we know what is being stored inside it.

In Python, we can assign a value to a variable by using an equals sign.

myage = 16 

So here, we are assigning the value of 16 to the variable “myage”. Now, we can use this variable in calculations:

print(myage + 10) 
>>>26

This code gives my age in 10 years time, which will be 26.

Data Types

So next, an important concept is a data type – and I will show you the 5 basic data types in Python. Data can have different types, for example it can be a number or text or just a true/false value.

Firstly, we have an integer which is a positive or negative whole number, which means it cannot have any fractions or decimals. E.g. 5, -5, 0 etc.

Next, we have a float which is a number with a decimal point, e.g. 8.26 or 3.0 or 0.0 etc.

x = 1
print(type(x))
>>> <class 'int'> 

In the example above, Python has named ‘x’ as an integer, because the number ‘1’ is a positive whole number.

x = 1.0
print(type(x))
>>> <class 'float'> 

But if we assign 1.0 to x, the data type will be a float because we have inserted a decimal point into the number.

Next, we have Boolean data types which are only 2 values, so a True or a False (or yes/no).

x = False
print(type(x))
>>> <class 'bool'> 

It is interesting to note that although a Boolean value is not actually a number, it is classified as an integer – where True has a value of 1 and False has a value of 0.

Next, we have strings which are used for letters, numbers, symbols or any other text. E.g. “I am learning to code” is a string (or ‘str’).

x = "I am learning to code"
print(type(x))
>>> <class 'str'> 

Lastly, we have a character which is simply a single letter, like ‘A’ or ‘s’ etc.

Operators

We will look at operators in Python which can be used to perform any mathematical calculations like addition, subtraction, division, multiplication. Let’s make two variables: num1 and num2, and do some mathematical calculations with them in Python; effectively creating a very basic calculator in Python!

num1 = 5
num2 = 10 

For addition, we print:

print(num1 + num2)
>>> 15

For subtraction, we get -5, because we have subtracted 10 from 5, thus we are left with a negative number.

print(num1 - num2)
>>> -5

For multiplication, we use an asterix (*) for a multiplication symbol:

print(num1 * num2)
>>> 50

For divsion, we use a forward slash symbol (/). NOTE: we will always get the answer as a float (so, a number with a decimal point). E.g. 6/2 = 3.0

print(num1 / num2)
>>> 0.5

A couple more useful operators are modulus, floor division and exponent.

Modulus is a % sign, and is used to find the remainder of any division. So if we do 10%3, the answer will be 1 because the number 3 goes into 10 three times, with a remainder of 1.

print(10 % 3)
>>> 1

Floor division is essentially the opposite to the modulus, because it gives the whole number value of the division, so 10//3 will give 3, because the number 3 goes into 10 three times.

print(10 // 3)
>>> 3

Finally, we use 2 asterix symbols for an exponent calculations.

print(10 ** 3)
>>> 1000

Conditional Statements

We are also going to look at python conditional statements because they are undoubtedly very important in all programming languages.

An IF statement uses logic to work decide which statement to run, and therefore to use an IF statement we use the following logical conditions:

To test for equality between num1 and num2, we use the double equals sign:

if num1 == num2: 

Greater than, less than:

if num1 > num2: (#greater than)
if num1 < num2: (#less than)
if num1 >= num2: (#greater than or equal to) 
if num1 <= num2: (#less than or equal to) 

Finally if we wanted to test for “not equal to”, we use an exclamation mark in front of the equals sign.

if num1 != num2: (#not equal to)

The IF statement follows the structure: IF a certain condition is TRUE, then run a specific statement (we use a colon (:) for then). Else if that certain condition is false, then run another statement.

I’ll walk you through an example to understand this concept a bit better:

Yash_age = 50
Scott_age = 20
If Yash_age > Scott_age: 
	Print(“Yash is older than Scott”)
Elif Yash_age == Scott_age: 
	Print(“Yash and Scott have the same age”) 
Else:
	Print(“Scott is older than Yash”)

So here, we expect that if we run this statement, we will get the output “Yash is older than Scott”.

The ELIF function is used in Python to say “if the previous condition was not true, then try this new condition…”

The ELSE function is used in Python to say “if the previous conditions were not true, then it must be this…”

FOR & WHILE loops

We will finish off this short introduction to Python by covering FOR loops and WHILE loops.

A FOR loop is used in python to iterate (or repeat) over a particular sequence of statements.

for i in range(4):

This Python syntax above iterates over a certain statement 4 times and then it will stop.

NOTE: In programming, by convention, we start counting from 0, NOT 1.  Let me show you an example:

for i in range(4):
    print("We are on number: " + str(i)) 

Here, we have concatenated a string and a variable (i) together using an addition symbol & the str() function, which converts the integer i into a string. This will iterate over the Print statement 4 times, however, it will start with i = 0, not 1.

So the output of this code would be:

>>>We are on number: 0 
We are on number: 1
We are on number: 2 
We are on number: 3

Lastly, a WHILE loop executes a set of statements while a certain condition holds true. In this way, a while loop is known as a preconditional loop because the condition is outlined right at the start of the loop, and only if this condition is true, then the next set of statements will be executed. If the original condition is false, then the set of statements will not be executed until the condition is true again.

age = 16 
while age < 18: 
   print(“Your age is:”, age, “ so, you are too young to drive. Please wait until you are 18 years old”)
   age += 1

This while loop checks that the age < 18, and if this condition is true, then it will print the string. After printing the string, the program will increment the age variable by 1, using the code “age += 1”, which is simply a shorthand (or syntactic sugar) for the code “age = age + 1”.

Conclusion

I hope that you learnt something new in this basic Python tutorial – although we have just broken the surface of this vast and versatile programming language, it is definitely a rewarding experience to get into coding Python.

If you have any questions, please do put them in the comments below this blog post and I will try my best to help you out.

Thanks for reading and enjoy coding! 

YouTube Video covering this Python Tutorial

Please see my video showing you the steps outlined above. Please do SUBSCRIBE to my channel and share with anyone who might find this useful! Thank you 🙂

How did you find this article?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Comment

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

This site uses User Verification plugin to reduce spam. See how your comment data is processed.
error: Sorry, content is protected! For special access, please email contact@bytesofintelligence.co.uk. Thank you.