Frequently Asked Question

Video: 6.2 Python basics
Last Updated 3 years ago


Press "Ctrl + F" to find the keyword of your interest.

If you wish to have a direct link access to the video timestamps, please follow these instructions.

Found this video helpful? Why not take the whole HIL Specialist course? A Certificate is waiting for you for free at HIL Academy.

Would you or your organization benefit from having these videos narrated in your native language? Contact us and let us know if you wish to contribute.

TRANSCRIPT

00:00:02

Hello and welcome to the Python Basics lesson.  

00:00:05

Here we will make a very brief overview of Python so you can get started with  

00:00:08

your test automation programming. If you want to learn more about Python,  

00:00:13

you can also find several free online resources about Python programing in the Materials tab. 

00:00:18

In this module, we will be using PyCharm to write and execute Python Code.  

00:00:23

A guide for how to set up your PyCharm environment is also available in the Materials tab. 

00:00:29

To get started, create a new project at the desired location and select the interpreter. 

00:00:34

Let's create our first test file by right-clicking on the folder's name,  

00:00:38

selecting "new" and "Python file".We can give any name for this file at  

00:00:43

this moment. Let's call it "code.py".In Python, just as in several other  

00:00:48

programming languages, we can compute different types of operations,  

00:00:52

such as relational, numerical, and multitype.The primitive or native datatypes in Python are  

00:00:58

integers, floats, and Booleans. As opposed to lower-level programming languages like C,  

00:01:04

Python does not require you to allocate variables before you use them for calculations.  

00:01:09

In Python, this is done automatically when the variable is used for the first time.  

00:01:14

The variable's datatype is automatically determined by the value you attribute to it. 

00:01:19

The "=" sign is used to attribute a value to a variable.  

00:01:23

In this case, let's make two variables. 

00:01:30

The first one is an "integer" type because it is a whole number,  

00:01:33

while the second is a "float" or "real" type, due to its decimal place. 

00:01:37

In Pycharm, you can use the key combination "Alt + Shift + e" to run the code you have  

00:01:41

selected in your file, or the line where your cursor is at. This has the same effect  

00:01:46

as writing that piece of code directly in the Python console. Throughout this lesson,  

00:01:51

we will be writing in the "code.py" file and then use this key combination to run the code. 

00:02:00

We can add or subtract these variables using the "+" and "-" signals and run the code,  

00:02:05

as you can see here. 

00:02:14

We can also add comments to our code. Comments are short notes saved in the code file which  

00:02:19

will not be considered when the file is run. This can be very useful in many situations, such as  

00:02:24

when you want to remind yourself, or let someone else know, what a specific section of code does,  

00:02:30

or when you want to exclude a certain section of code when testing or troubleshooting. 

00:02:34

To add one-line comments we use the "#" symbol at the beginning of the sentence. 

00:02:41

To comment on more than one line, we can write the comment between triple quotes. 

00:02:56

Now let's look at a cluster of Python primitive variable types, called sequences. Sequences are  

00:03:02

a set of elements, which can be in the form of a String, a list, a tuple, or a dictionary. they are  

00:03:08

the list, the tuple, the dictionary and the.To create a String, you can use either single or  

00:03:11

double quotes. You can also concatenate Strings by adding them together. 

00:03:33

An easy way to format a string with some variable values, is to use the "f" letter before quotes. 

00:03:47

Another way to run the code is by right clicking on the file and selecting "run". 

00:03:54

As you can see, nothing is displayed. In order to show messages in the console,  

00:03:59

we can use the "print" function. You can use it to print strings, values,  

00:04:04

and many other variable types. Multiple values can be printed by separating them with a comma. 

00:04:33

Now let's take a look at some relational operands we can use to compare two integers,  

00:04:37

floats, or even Strings. We use the "==" to check if two values are  

00:04:48

equal, "!=" to check if two values are different, and the inequality operators ">",  

00:04:54

">=", "<", or "<=". These operations return a Boolean result: True or False. 

00:05:09

Once we have these Boolean values, we can also do logic operations in Python on them,  

00:05:14

using the reserved words and, or, and not. 

00:05:27

Now let's create a list. To do that, use "[]" adding all variables separated by ",". 

00:05:43

Lists are mutable variables, which means you can change them.  

00:05:47

We can add new variables by using the ".append" method. 

00:05:51

The list, called var_list, is created with 4 elements. The .append method adds a new  

00:05:56

element at the end of the list already created. If we print the list again, we can see that the  

00:06:01

new elements were successfully added.Adding lists will merge them together,  

00:06:10

while multiplying lists replicates them. 

00:06:15

It is also possible to access an item from the end of the list: 

00:06:27

Or select several items by slicing it. 

00:06:40

Tuples are similar to lists and also accept any datatype,  

00:06:43

but the main difference is that once created, they cannot be changed,  

00:06:47

such as by appending. This property is called immutable. To create a tuple, we use "()". 

00:06:54

The last sequency type is a dictionary. To create a dictionary, we use "{}". The information added  

00:07:01

to a dictionary needs to be a key, followed by a ":" and the value to be attributed to  

00:07:05

that the key. To add more elements, we follow the same rule of separating each new item by ",". 

00:07:15

You can access the values of items inside lists, tuples, and Strings with their index.  

00:07:21

Note that in Python, they start from zero.  

00:08:17

Meanwhile, you can access values from dictionaries using their keys. 

00:08:24

Now that we know the basic data types, we can look into some basic code flows.  

00:08:29

While there are several pre-built functions in Python, here we will look at just the "if/else",  

00:08:34

"for", and "try-except" functions.The if/else structure in Python  

00:08:38

can be used as a decision-making statement. Note that, as opposed to C,  

00:08:43

Python uses indentation to define blocks of code and what is inside each statement. 

00:09:06

This simple structure uses the value of "x" to print the correct message. 

00:09:16

The "for" statement is useful for repeating a specific block of instructions,  

00:09:20

to navigate through a list, or to repeat a section of the code for a restricted number of times.  

00:09:25

In Python, you iterate over each item inside the given variable. Hence,  

00:09:29

the length of the given variable will define the number of times the "for loop" will run. 

00:09:34

Let's look at an example. We'll create a list "my_list" as shown here. 

00:09:36

When we run this code, the program will iterate over the "for" loop several times. Each time one  

00:09:42

list element will be selected and then assigned to the new variable item, as declared in the  

00:09:46

statement "for item in "my_list". Then, each item is printed in the console. Let's see the result. 

00:09:56

We can also run the for loop for a predefined number of times,  

00:10:00

using the "range" function. Let's run a loop 10 times, adding a variable to itself each time. 

00:10:25

Now let's look at why try-except is important. Python is an interpreted  

00:10:29

and not a compiled language. This means the code is not verified prior to execution.  

00:10:35

In some cases, errors can happen which will cause the code to abort.  

00:10:39

In order to avoid aborting a program, any error type can be captured individually and treated in  

00:10:44

order to continue with code execution. As usual in Object Oriented Languages,  

00:10:49

Python has the structure try-except to catch errors and treat them. 

00:10:54

In this example, we can visualize on the Python console the KeyError exception raised because  

00:11:00

the code tries to access a dictionary key that was not properly declared. 

00:11:23

To avoid this error, let's add a try-except structure.  

00:11:26

Here, let's specify the type of error we want to treat.  

00:11:30

If we want to treat any error the same way, you can just remove 'KeyError'. In this example, let's  

00:11:35

print out the existing keys in the dictionary and add a new key with the "None" value. 

00:12:16

Now let's take a look into custom functions. In Python, we can self-define functions for any  

00:12:22

variable; meaning we don't need to specify what is the type of the parameters or the type of return.  

00:12:27

The "def" statement defines the description of the function, followed by its name,  

00:12:31

and the arguments and/or key arguments inside parentheses.  

00:12:35

In this example, let's create a function to compute the power of the input, plus an offset. 

00:12:47

This function has a mandatory argument, "number", and two optional key arguments:  

00:12:52

"exp" and "offset". If we simply call "2", for example, the returned value is 4,  

00:12:59

since both "offset" and "exp" were not defined, so their default values are used.  

00:13:04

By changing "offset" or "exp", we can get different values, as expected. As you can see,  

00:13:10

it's not necessary to put the arguments in order when specifying the names of the key arguments. 

00:13:15

In the final case, we get a different result to the previous two. This is because we didn't  

00:13:20

specify the key arguments, so Python simply used the order originally declared in the function,  

00:13:26

which was "number", then "exp", and then "offset". 

00:13:31

With this, we've covered most of the basic functionalities built  

00:13:34

in Python that will help us with test automation.  

00:13:37

In the next lesson, we will look at some of the important freely available packages that extend  

00:13:42

this functionality and serve as a basis for our test automation tests.

Please Wait!

Please wait... it will take a second!