Frequently Asked Question
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
Hello and welcome to the Python Basics lesson.
Here we will make a very brief overview of Python so you can get started with
your test automation programming. If you want to learn more about Python,
you can also find several free online resources about Python programing in the Materials tab.
In this module, we will be using PyCharm to write and execute Python Code.
A guide for how to set up your PyCharm environment is also available in the Materials tab.
To get started, create a new project at the desired location and select the interpreter.
Let's create our first test file by right-clicking on the folder's name,
selecting "new" and "Python file".We can give any name for this file at
this moment. Let's call it "code.py".In Python, just as in several other
programming languages, we can compute different types of operations,
such as relational, numerical, and multitype.The primitive or native datatypes in Python are
integers, floats, and Booleans. As opposed to lower-level programming languages like C,
Python does not require you to allocate variables before you use them for calculations.
In Python, this is done automatically when the variable is used for the first time.
The variable's datatype is automatically determined by the value you attribute to it.
The "=" sign is used to attribute a value to a variable.
In this case, let's make two variables.
The first one is an "integer" type because it is a whole number,
while the second is a "float" or "real" type, due to its decimal place.
In Pycharm, you can use the key combination "Alt + Shift + e" to run the code you have
selected in your file, or the line where your cursor is at. This has the same effect
as writing that piece of code directly in the Python console. Throughout this lesson,
we will be writing in the "code.py" file and then use this key combination to run the code.
We can add or subtract these variables using the "+" and "-" signals and run the code,
as you can see here.
We can also add comments to our code. Comments are short notes saved in the code file which
will not be considered when the file is run. This can be very useful in many situations, such as
when you want to remind yourself, or let someone else know, what a specific section of code does,
or when you want to exclude a certain section of code when testing or troubleshooting.
To add one-line comments we use the "#" symbol at the beginning of the sentence.
To comment on more than one line, we can write the comment between triple quotes.
Now let's look at a cluster of Python primitive variable types, called sequences. Sequences are
a set of elements, which can be in the form of a String, a list, a tuple, or a dictionary. they are
the list, the tuple, the dictionary and the.To create a String, you can use either single or
double quotes. You can also concatenate Strings by adding them together.
An easy way to format a string with some variable values, is to use the "f" letter before quotes.
Another way to run the code is by right clicking on the file and selecting "run".
As you can see, nothing is displayed. In order to show messages in the console,
we can use the "print" function. You can use it to print strings, values,
and many other variable types. Multiple values can be printed by separating them with a comma.
Now let's take a look at some relational operands we can use to compare two integers,
floats, or even Strings. We use the "==" to check if two values are
equal, "!=" to check if two values are different, and the inequality operators ">",
">=", "<", or "<=". These operations return a Boolean result: True or False.
Once we have these Boolean values, we can also do logic operations in Python on them,
using the reserved words and, or, and not.
Now let's create a list. To do that, use "[]" adding all variables separated by ",".
Lists are mutable variables, which means you can change them.
We can add new variables by using the ".append" method.
The list, called var_list, is created with 4 elements. The .append method adds a new
element at the end of the list already created. If we print the list again, we can see that the
new elements were successfully added.Adding lists will merge them together,
while multiplying lists replicates them.
It is also possible to access an item from the end of the list:
Or select several items by slicing it.
Tuples are similar to lists and also accept any datatype,
but the main difference is that once created, they cannot be changed,
such as by appending. This property is called immutable. To create a tuple, we use "()".
The last sequency type is a dictionary. To create a dictionary, we use "{}". The information added
to a dictionary needs to be a key, followed by a ":" and the value to be attributed to
that the key. To add more elements, we follow the same rule of separating each new item by ",".
You can access the values of items inside lists, tuples, and Strings with their index.
Note that in Python, they start from zero.
Meanwhile, you can access values from dictionaries using their keys.
Now that we know the basic data types, we can look into some basic code flows.
While there are several pre-built functions in Python, here we will look at just the "if/else",
"for", and "try-except" functions.The if/else structure in Python
can be used as a decision-making statement. Note that, as opposed to C,
Python uses indentation to define blocks of code and what is inside each statement.
This simple structure uses the value of "x" to print the correct message.
The "for" statement is useful for repeating a specific block of instructions,
to navigate through a list, or to repeat a section of the code for a restricted number of times.
In Python, you iterate over each item inside the given variable. Hence,
the length of the given variable will define the number of times the "for loop" will run.
Let's look at an example. We'll create a list "my_list" as shown here.
When we run this code, the program will iterate over the "for" loop several times. Each time one
list element will be selected and then assigned to the new variable item, as declared in the
statement "for item in "my_list". Then, each item is printed in the console. Let's see the result.
We can also run the for loop for a predefined number of times,
using the "range" function. Let's run a loop 10 times, adding a variable to itself each time.
Now let's look at why try-except is important. Python is an interpreted
and not a compiled language. This means the code is not verified prior to execution.
In some cases, errors can happen which will cause the code to abort.
In order to avoid aborting a program, any error type can be captured individually and treated in
order to continue with code execution. As usual in Object Oriented Languages,
Python has the structure try-except to catch errors and treat them.
In this example, we can visualize on the Python console the KeyError exception raised because
the code tries to access a dictionary key that was not properly declared.
To avoid this error, let's add a try-except structure.
Here, let's specify the type of error we want to treat.
If we want to treat any error the same way, you can just remove 'KeyError'. In this example, let's
print out the existing keys in the dictionary and add a new key with the "None" value.
Now let's take a look into custom functions. In Python, we can self-define functions for any
variable; meaning we don't need to specify what is the type of the parameters or the type of return.
The "def" statement defines the description of the function, followed by its name,
and the arguments and/or key arguments inside parentheses.
In this example, let's create a function to compute the power of the input, plus an offset.
This function has a mandatory argument, "number", and two optional key arguments:
"exp" and "offset". If we simply call "2", for example, the returned value is 4,
since both "offset" and "exp" were not defined, so their default values are used.
By changing "offset" or "exp", we can get different values, as expected. As you can see,
it's not necessary to put the arguments in order when specifying the names of the key arguments.
In the final case, we get a different result to the previous two. This is because we didn't
specify the key arguments, so Python simply used the order originally declared in the function,
which was "number", then "exp", and then "offset".
With this, we've covered most of the basic functionalities built
in Python that will help us with test automation.
In the next lesson, we will look at some of the important freely available packages that extend
this functionality and serve as a basis for our test automation tests.