Sunday, December 22, 2019

Python tutorial 15 | Exception handling part 1 | Try except statement

Let us go to an important tutorial in the Python Tutorials for Beginners. This Python beginner tutorial explains exceptions in Python and how to handle them. Please view the Python video tutorial 15 or read on...First, what are exceptions? Exceptions are the errors that are thrown during script execution. Exceptions are different from syntax errors, which are thrown before script execution. If we do not handle an exception, our script execution stops with an error message. Let us see some Python exception examples.

# Python code
print (i * 2)   # throws NameError exception because variable 'i' is not defined
int ('Hi')   # throws ValueError exception because 'Hi' is an invalid argument to the int function
print (1 / 0)   # throws ZeroDivisionError because division by zero is not allowed in Python programming

We can handle exceptions in Python using the Python try except statement. This means that we can protect our Python script from stopping abruptly. The Python try statement syntax is:
try:
    try clause
except named exception:
    except clause

In the above try statement format, the try clause is a code block with one or more statements in it. Same for the except clause. When Python runs the try clause, there are three possibilities 1), 2a) or 2b):
1) If there is no exception, the try clause is completed and the except clause is skipped.
2) If an exception is thrown in the try clause, the remaining statements in the try clause are skipped. Python matches the thrown exception with the named exception(s) after except:
2a) If the exception matches, the except clause  is run.
2b) If the exception does not match, the Python script execution stops with an error message.

Now, let us see the Python try except statement example.

# Python code example
try:
    i = 10 # comment this line to generate an exception
    print (i*2)
except NameError:
    print ('Please define the variable first.')

The Python try statement syntax can also have multiple excepts:
try:
    try clause
except named exception 1:
    except clause 1
except named exception 2:
    except clause 2
.
.
.
except named exception n:
    except clause n

In the above try statement format, there are multiple except clauses within the try statement. When Python runs the try clause there are three possibilities 1), 2a) or 2b):
1) If there is no exception, the try clause is completed and all the except clauses are skipped.
2) If an exception thrown in the try clause, the remaining statements in the try clause are skipped. Python matches the thrown exception with the named exception(s) after each except:
2a) If the exception matches any named exception, only that except clause  is run (and all the other except clauses are skipped).
2b) If the exception does not match any named exception in any except, the Python script execution stops with an error message. In order to catch any unnamed exception, we can have the last except without any named exceptions (as in the Python code examples below).

Sunday, December 15, 2019

Tue, Dec 17 | Test Automation Frameworks with AI and ML | Advanced Live Webinar

Here is the last session in 2019 on Test Automation frameworks, useful for professionals who are seriously interested in test automation and automated testing. This live webinar will be hosted by Testim's software developer, Benjamin Gruenbaum and is titled, Is AI Taking Over Front-End Testing? What’s visible on the screen is the only thing that matters to end users. To ensure an impeccable graphical user interface (GUI), front end testing is a must. The contents of the session include What is browser testing and why it’s hard to automate, Evolution of Test Automation Frameworks, Fundamentals of AI and ML and How ML can be applied to test automation.

Tue, Dec 17, 2019 (1-hour duration) 
9 am PST/ 12 pm EST / 5 pm UTC / 10:30 pm IST
* If you register but cannot attend the live event due to some reason, you will be still able to see it's recording later.

Wednesday, December 11, 2019

Python tutorial 14 | File handling | Working with files

Moving on to the next tutorial in the Python Tutorials for Beginners. This Python beginner tutorial explains how to work with files. Please view the Python video tutorial 14 or read on...Let us learn how to work with text files in Python programming. The syntax for opening a file with the open function is: file = open (file_name, mode)

In the above syntax, the mode is optional. If the mode is not specified or specified as 'r', the file will open in the read mode. If the mode is specified as 'a', the file will open in the append mode (meaning we can add text after existing text in the file). If the mode is specified as 'r+', the file will open in the read-write mode (meaning that we can read from the file and write to it). If the mode is specified as 'w', the file will open in the write mode (meaning that the existing text will be erased, so we have to backup the text and be careful).

Now, let us see the file functions' syntax.
file_name.read (n bytes) - read n bytes from the file into the memory
file_name.read() - read the whole file into the memory
file_name.readline() - read one line from the file
file_name.readlines() - read all lines from the file into a list
file_name.write (string) - write a string to the file (and return the number of characters written)
file.close - close the file (end the system connection to the file)

Now, let us see Python examples of file functions.

Monday, December 2, 2019

Live Webinar | Why Test Automation Fails | Learn Test Design and Implementation Tips

Here is an opportunity to join an advanced live webinar on Test Automation. Learn about the reasons that may lead to test automation failure. And learn test design and implementation tips towards successful test automation. This webinar will be a deep dive into common test automation failures with actionable tips on how to design end-to-end tests aiming at test automation project success.

Register Now (only limited seats are left) at https://tinyurl.com/TestAutomationWebinar
Tuesday, Dec 10, 2019 (1-hour duration)
9 am PST / 12 pm EST / 5 pm UTC / 10:30 pm IST

Test Automation Webinar

Sunday, December 1, 2019

Python tutorial 13 | List functions

Moving on to the next tutorial in Python Tutorials for Beginners. This Python beginner tutorial is the next part of the Python tutorial 12 | Lists. This tutorial explains many Python list functions with examples. Please view the Python video tutorial 12 and Python video tutorial 13. Or read on...

Let us learn about list functions in Python programming with code examples. The Python list functions are explained as comments (starting with the # symbol in the Python code below). These list functions work on Python lists with different data types (integers, floats or strings), as appropriate.

# Python code examples
# create a list of strings
vowels = ['a', 'e', 'i', 'o', 'u']

# len function returns the number of items in a Python list
# print 5, which is the number of items in the vowels list
print (len(vowels))

# min function returns the smallest item in a Python list
# print 'a', which is the smallest item in the list
print (min(vowels))

# max function returns the largest item in a Python list
# print 'u', which is the largest item in the list
print (max(vowels))

# index list function returns the index of a specific item in a list; indexes start from 0
# print 1, which is the index of item, 'e' in the list
print (vowels.index('e'))

# count list function returns the number of occurrences of a specific item in a list
# print 1, which is the number of times 'i' appears in the list
print (vowels.count('i'))