How To Learn Python 3 For Beginners By Tutorial Online

How To Learn Python 3 For Beginners By Tutorial Online

Hello everyone and welcome to Prograp! Today we have for you our Python Tutorial Online for Beginners,  this tutorial has basic examples that will help you to understand more about how to use and what you can do with Python, For our purposes we will use some examples taken from well known books and websites that will give you an introduction to Python programming. Our examples are constructed in such way that you can increase your knowledge from zero to an amateur programmer level. It will allow you to build your own way to a deep understanding and management of the resources that Python has available for you. Now let's prepare to learn and improve our programming skills in Python with Prograp just for you.

A first approach to Python: Using Python as a Calculator

Our first programs will be related to programs that allow us to calculate quantities, it means that our first approach to Python will be "Python as calculator". At first we'll learn how to do simple calculations, such as you wanna estimate the height in a given time achieved by a stone which is thrown to the air with an initial speed and it's subject to the acceleration of the Earth, in this case we can use the following simple model to estimate the height.

Here we show a basic model from physics to estimate the height using Python as calculator

Suppose we know that the initial height is Vo = 30 m/s, furthermore we will consider that the initial height is Yo=0 m and we will take the gravity magnitude as equal to g = 9.8 m/s**2, so if we wanna estimate the height achieved by the ball after  t= 4s then we can use the Python calculator directly, This exercise will show us how to use the basic mathematics operations +,-,*,/ and exponentiation which will be written using double asterisk ** , for example we wanna calculate 5*5*5*5, this is the same that print 5**4, so let's see how it look in our IDE.

Here you can see the result from a calculation performed with Python.

As we can see here we employed the operations mentioned before and we used the command print, this command help us to visualize the operations performed by the computer, although we can put the operations directly in our IDE it's better to get use to print our results because it will be fundamental in our following lessons.

Performing simple calculations

A program can be defined as a sequence of instructions to a machine as a computer, written in a given programming language. This sequences are subject to a limited language programming so in order to perform a task in the computer we have to use long chains of instructions to achieve our expected result, the computer will perform only what it's implied by the program.

As all learning processes, learning a programming language implies consider strategies and understand the programming environment, it implies that we need to get use to this environment by means of training and programming. This task takes time and it will be good to have a set of strategies that help us to accomplish our goal, at first we need to understand what tools do we need to solve a given problem, Second we need to express our solution to the given problem by means of a sequence of instructions and store them in the computer. Third we need to understand how to check the validity of our results and finally we need to analyze and rack our mistakes and correct them. In order to achieve this we need to practice creating a lot of programs.

Now it's time to focus on using Python as a calculator and understand more deeply the basic arithmetic done with Python.

Here you can see simple calculations done using Python

Figure 1. Calculating using Python platform.

Here we can see the details about arithmetic operations made in Python, at first we don't need to worry about the -'>>>'- that appears at the left of the screen, this is taken by defect and helps you to know where you have to begin writing your program. As a calculator you only need to enter the numbers and the operation that you wanna perform with them, then you only have to click enter and then in the next line you will have the result of your operation, only you will have an expression if the instruction is understandable, otherwise you will receive a message in red advising about a Syntax error.

In the first program you can see a set of operations, they show the use of +,-,*,/, as we can see Python follows the known rules of arithmetic, for example if we consider the expression 9*5-8 then based in our mathematical knowledge at first we need to perform the multiplication and then the subtraction, that why, as our reader can check, if we type 7+8*3-4*5 then we will obtain 11, as shown by Python as well in our first picture.

Some mathematical operations that we consider here are the floor function and the remainder function, for example if calculate 25/7 then the result can be see as 3.571428..., here ... denotes the fact that this number has an infinite and periodic expansion. so we can conclude that Floor(25/7)=3, this can be seen from 25//7=3. Another function that appears is the function remainder denoted by '%', if we calculate 25%7 and given that 25=7*3+4, then 25%7=4, it means that the mentioned function obtain the remainder from the division.

We continue performing basic operations with Python

Figure 2. Python as a calculator.

As you could see, expression such as >>>23///4.2 didn't make sense to Python, the reason is that /// is not an operation for Python so there is no result and the system just say Syntax Error: invalid syntax. An non expected mistake is if we put >>>  4+9, here red space denotes a common space left in the interpreter, as we can see from the screen Python replied to this line Syntax Error: unexpected indent,

so it was not possible to obtain the expected answer. This is an advice to our future programming, we should respect the indentation from Python as we will explain in another article.  At final point we can see that the we can perform comments in our screen that doesn't affect the programming, they will be written using as first step '# This is my first comment ;). This is all you need to know to use Python as a calculator, in the next step we will learn how to perform Algebra with our variables and How to write simple programs.

How to run a program in Python?

In order to obtain the result of the program we need to be able to run it using Python in our case. The computer is different from the human in a vast amount of aspects, suppose that you are taking a shower and then your read the instructions of your shampoo bottle, it would say "Open the bottle, release the shampoo on your hand and then use in your head", until here if we follow the instructions then we will open as well and we'll use the product, but what will happen if we give this instruction to the computer? Let's say that our computer was able to understand what means shampoo, hand and all the implied concepts and that our computer have a physical body like a robot. If the computer tries to follow the order then ................. it will never stop. It happens because we provided an ambiguous order, we didn't provide a stop criteria to the computer, so after it uses the product and release on it's head then it will repeat the process again and again. What is the problem here then? Simple, computers doesn't behave like humans, in order to make a computer to do something we need to define very well each instruction without ambiguities and following the semantic and syntax of the programming language used.

We will discuss that aspects more deeply later, for now let's learn how to run our program in Python, you can copy and paste the following code extracted from https://gist.github.com/jurandysoares/4380835 import turtle and obtained as well from the Introduction a la programación en Python I: Aprendiendo a programar con Python offered in Coursera by the Pontificia Universidad Católica de Chile.

#Codigo extraido de: https://gist.github.com/jurandysoares/4380835
import turtle
screen = turtle.Screen()
screen.setup(800,600)
circle = turtle.Turtle()
circle.shape('circle')
circle.color('red')
circle.speed('fastest')
circle.up()
square = turtle.Turtle()
square.shape('square')
square.color('green')
square.speed('fastest')
square.up()
circle.goto(0,280)
circle.stamp()
k = 0
for i in range(1, 17):
    y = 30*i
    for j in range(i-k):
        x = 30*j
        square.goto(x,-y+280)
        square.stamp()
        square.goto(-x,-y+280)
        square.stamp()
    if i % 4 == 0:
        x =  30*(j+1)
        circle.color('red')
        circle.goto(-x,-y+280)
        circle.stamp()
        circle.goto(x,-y+280)
        circle.stamp()     
        k += 2
    if i % 4 == 3:
        x =  30*(j+1)
        circle.color('yellow')
        circle.goto(-x,-y+280)
        circle.stamp()
        circle.goto(x,-y+280)
        circle.stamp()
square.color('brown')
for i in range(17,20):
    y = 30*i
    for j in range(3): 
        x = 30*j
        square.goto(x,-y+280)
        square.stamp()
        square.goto(-x,-y+280)
        square.stamp()     
        turtle.exitonclick()

After you put the code in your IDE platform or in your command prompt then you can press Run or pushenter respectively

Explanation with IDEabout how to run a program using Python.

in the first case you will see the following picture created using Python.

Here you can find the result of the code done on Python of a Christmas tree.

Now you have a digital version of a Christmas Tree, Merry Christmas for you! Now we feel happy for you, this is a great step toward the to learn Python and any programming language, be able to run a program, if you want try to play with the code a little bit or if you care about the meaning of this code don't worry for now, we will study this kind of programs more deeply in the next articles which will give you the basics about Expressions and Variables in Python. Thanks for reading us, we hope you can keep on your way to learn Python with Prograp just for you!

Kamiko

Hi, everyone! I am just a girl who is a beginner to study web and programming. Sometimes, knowledge of Internet makes us confused, so I will explain such the topics to make you understand very well through the eyes of a beginner. Hopefully, my contents will help you to be sucessful and give you joy and fun!

No comments:

Post a Comment