JUnit 5 : Environment Setup


Let us discuss on JUnit 5 environment setup in Java Application.

Pre-Requisite

JUnit 5 does not required any major setup. It’s required following library and IDE:

  • JDK : Min 1.8 or above
  • IDE : Eclipse/STS or Netbean or IntelliJ
  • Latest version of Junit 5 is readily available with latest version of IDE

More :

Junit 5 Setup in Java Application

You can add JUnit 5 library in your Java Application by follow below steps:

  • Right clock on your project -> Properties->Build Path-> Configure Build Path
  • Select Junit Library by following below steps in screen shot.
Junit 5 Environment Setup
  • After clicking in Next button (Step 5). You will get below screen where you can select Junit 5.
JUnit 5 Environment Setup
  • After click on Finish button (Step 7) this JUnit 5 library will add on your application.
  • Now you can click on “Apply” or “Apply and Close” button to complete this process.

Now your application is setup to write test JUnit 5 test cases.

In further topic you will learn about how to write JUnit Test Cases.

Junit 5 : How to write JUnit Test Cases?


In previous topic you learn about the JUnit setup in Java Application. In this topic you will learn about the Junit test cases writing of methods for particular java class.

Lets discuss about the test cases writing in Java application.

  • Right click on your java project -> Select New -> Junit Test Case as below.
How to write Junit Test Cases in Java application
  • After selecting the Junit Test Cases you will get below screen. Here the Jupiter option (Step 1) is for implementing the Junit Test cases with Junit 5. You can write test class name (Step 2) as CalcularTest and also select Fixture (Step 3) if initialization required for your test cases.
How to write Junit Test Case with Junit 5
  • Instead of writing the test cases, If you want to generate the Test cases for particular class then select browse (step 4) option and you will get below screen where you can search for class.
How to generate Junit test cases for particular class
  • After selecting class, click on OK button. You will get next screen with names of method where you can select methods for which you want to generate the test cases.
How to generate Junit test cases
  • After selecting methods click on Finish button, you will see a class CalculatorTest.java class added in you application with some test cases code as below.

CalculatorTest.java

JUnit Test Cases Generated
  • Now your some test cases generated with some dummy code where you can write your own logic inside of these test method by using assertion or assumption and run these test cases.

How to run Test Junit Cases?

Once you will done with the test case writing. You can follow these steps to run test cases:

  • Right click on your Java Application->select run as -> Junit
  • After executing the test cases you will see the test case result as below with red and green colors. Where green represent success and red as failure of test cases.

Junit Failed Cases

Junit some test cases failed

JUnit Success Case

Junit all success

Here I have done some changes in auto generated test cases as below and after execution showing as success as above. You can try with same or update it.

JUnit for success case

In the further topic you will learn more about the Junit 5 Annotations, Fixtures, Assertions and Assumptions in detail with examples.

Junit 5 : Architecture


Junit 5 Architecture mainly divide in three modules:

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

JUnit 5 Architecture

In Junit 5 Architecture each module having specific purpose as below:

JUnit Platform

Junit Platform is core of JUnit 5 which provides a bunch of libraries that provide Console Launcher to launch a platform from the command line and invokes JUnit Runner for running Test Engine in JUnit 4 based environment.

JUnit Jupiter

Jupiter provides the APIs for developer to interact assertions and assumptions instead of Runner itself.

JUnit Vintage

By default, JUnit 5 does not provide backward compatibility, instead, it uses another module called as Vintage which provides APIs to write test cases supported by older version JUnit 3 or Junit 4.

Extensions (Third Party)

JUnit 5 supports third-party APIs also on its platform with the help of the Extensions module.

In further topics you will learn about the JUnit 5 Annotations and Implementation of test cases by considering different scenarios.

JUnit 5: Introduction


In the previous topic you learned about the JUnit introduction and compare between different Junit framework like Junit vs TestNG. Here we will discuss about some important points to remember about Junit 5.

  • JUnit 5 is the latest version of JUnit which require a minimum JDK 8 version.
  • Junit 5 supports popular IDEs such as Eclipse/STS and IntelliJ for test case executions.
  • Junit 5 supports all new Java Language features including Lambdas.
  • Junit 5 provides various assertions and assumptions.
  • Junit 5 provides a whole new architecture that is comprised of modules, making it suitable to be used in small memory-based systems.
  • Junit 5 vintage module supports backward compatibility with previous version Junit 3 and Junit 4.

In the further topic you will learn about the JUnit 5 Architecture, JUnit 5 Annotations and Implementation of test cases by considering different scenarios.

JUnit Introduction


  • JUnit is an open-source framework for Java programming language, used to write and run automated unit test cases. It is widely accepted as a standard for unit testing in Java.
  • JUnit is a simple testing framework created by Kent Beck and Erich Gamma during a long airplane trip.
  • It is released under IBM’s common public license version 1.0 and hosted on SourceForge.
  • Till JUnit 3.8 there were no stable versions.
  • It is the Java version of the xUnit architecture for unit and regression testing frameworks
  • According to a survey of 2013, 10,000 Java projects hosted on GitHub were using JUnit as a testing framework.
  • In this tutorial, we will perform unit testing with JUnit 5 which is the latest stable JUnit version.

In the further topics we will discuss more about the Junit basics and implementation of test cases by using JUnit 5 after considering different scenarios and backward compatibility with previous version JUnit 3 and JUnit 4.

Unit Testing


Unit testing is process of Software development which validate that each unit/line of code is performing as expected or not. On broader prospect Unit Testing is defect prevention and detection strategy to reduce software development risks, cost, and time to deliver a quality software.

Developers write Unit Test cases which covers the functionality of specific section of code, function level, class level and branches level so that ensure the implemented code is working as expected. Unit testing not completely verify the functionality of software but it’ ensure that building block of software work independently as expected from others.

Why Unit Testing Framework?

In Unit Testing, we verify each section of code for success and failure scenarios where need to implement the conditions in code Test code by considering the different cases.

Let’s take the below examples of Calculator.java class on addition() method where need to verify output of result by considering different test cases for success and failure.

Calculator.java

public class Calculator {
		public int addition(int a, int b) {
			return a + b;
		}
	}

Test Cases Implementation

In this case for testing this method we need to create one test class i.e CaculatorTest.java and implement the condition for all scenarios of every method test as follows:

Success Case: Pass arguments as a=2, b=3 return result=5, Expected Result =5

Failed Case: Pass arguments as a=2, b=3 return result=Other, Expected Result=5

class CalculatorTest {
private static final Logger logger = Logger.getLogger(CalculatorTest.class.toString());
private Calculator calc = new Calculator();
		void additionTest() {
		int sum = calc.addition(2, 3);
		if (sum == 5) {
		logger.info("Addition method test has passed.");
		} else {
		logger.info("Addition method test has failed.");
			}
		}	
		public static void main(String[] args) {
			CalculatorTest cTest = new CalculatorTest();
		cTest.additionTest();
	}	
	}

Here if you noticed for testing a single small addition() method required to write these many lines of code for creating object of Calculator class, calling method and check for pass and failed scenarios. Apart from it you need monitor the console logs also for verify the expected result. Think of when there are multiple methods in class and software having lots of classes on that case writing Junit code is cumbersome process for developers.

From the above example, Now analyze tasks that need to be done manually with and without framework :

TaskWithout FrameworkWith Framework
PreparationManually Manually
Provide Test Inputs Manually Manually
Run the tests Manually Framework
Provide expected outputs Manually Manually
Verify results Manually Framework
Notify developer if test failed Manually Framework

Now it should be clear that how important to use framework because it saves significant amount of time of developers.

There are lots of framework for Unit Testing but when we talk about the Java unit testing mainly these two are most popular.

  1. Junit
  2. TestNG

TestNG is a testing framework for Java and is inspired by JUnit and NUnit which covers a wide range of testing like unit testing, functional testing, integration testing, and system testing.

In the further topics we will discuss more about the Junit basics and implementation of test cases by using Junit 5 after considering different scenarios.

Software Testing


What is Software Testing?

When developer create a new software and deploy in market for use of public. The success of software depends on popularity of software, but it becomes terrible when users complains bugs, issues in the software. In this case developer need to debug the issues and fixed again for release the software that’s time-consuming process.

To overcome this practice “Software testing” comes in picture. This activity makes sure that every functionality or component of software is working as expected and outcome of software is as expected. The main purpose of software testing is delivered as defect free software.

Why Software Testing?

“Prevention is better better than care”

This phrase is very well suite for software development, If developers do the software testing on the time of development then all the issues will identify and fixed on the time of development which will make the bug free software. Software testing saves a lot of time for developers and reduces the frustration level caused by detecting bugs after deployment.

Software Testing Types

Software testing mainly categories in two types:

Software Testing Types
  1. Manual Testing: In this testing strategy is done manually by a tester to discover the bugs in software and check the results. This is also required because 100% automation testing is not possible.
  2. Automated Testing: In this testing, scripts and codes are written by testers to automate the test cases execution. This testing strategy saves lots of time as well manpower and generate report by automation tool.

Software Testing Hierarchy

In software testing, whether you choose automation or manual testing it can’t complete on one go. It comprises through 4 testing levels as below:

Software Testing Hierarchy

Unit Testing

Unit testing covers all independent parts or components of code i.e. known as Units. Unit testing is the first level of testing which perform on time of the development phase by developers while developing the software.

Integration Testing

Once the unit testing is working fine of all units of code then testing will be preform after integrating them i.e. called the integration testing. In this testing we check the communication between each unit are working as expected or not.

System Testing

Once the integration testing completed for all modules, then testing start for complete software i.e. called as System testing.

Acceptance Testing

After completing the System testing the software is evaluate as per business requirements i.e. called as Acceptance Testing. Once acceptance testing is completed successfully the software deployed to market for general user’s access.

The above testing hierarchy is very high level, there are other sub phases also for software testing, you will get to know by following this link https://en.wikipedia.org/wiki/Software_testing

In the further topics we will discuss more on Unit Testing by using Junit 5 and implementation on time of development.

Junit: Jacoco Code Coverage Plugin Integration with Eclipse/STS


In previous post you learn about the Junit Code Coverage Report generation by using Maven POM.xml that’s help to generate report for auditing purpose and check the status of junit test case implementation but everytime for these report you have to visit directory target/site/jacoco/index.html or target/jacoco-report/index.html

To reduce this effort, we can add this Jacoco plugin our eclipse/STS IDE and directly check the status over IDE.

Integration of JACOCO Code Coverage Plugin in Eclipse/STS

You can follow these steps to integrate this JACOCO code coverage plug in you eclipse or STS IDE.

  • Go to IDE top header Help->Eclipse Market Place it will open the pop up for Eclipse Market Place.
  • Type text as “Code Coverage” in search text box and click “Go” button. You will get below screen
JACOCO Code Coverage Plugin for Eclipse/STS
  • You will get list of plugins select the plugin “EclEmma Code Coverage” click on Install button for this plugin.
  • You will get next screen where need to accept terms and conditions and click continue.
  • After installing this code coverage plugin, It will ask to restart your IDE.
  • After restart your IDE, go to the top Windows->Show View->Others then search for “Coverage” as below then select option click on Open button.
  • You will see one additional tab “Coverage” on your console view section as below.
Coverage tab on view
  • Also get “Coverage As” option as below when right click on your application. Now you can run you Junit.
JACOCO coverage as option
  • Once you run your application Junit by using “Coverage As” option you will see the results on “Coverage” tab as below.
JUnit Code coverage report by Eclipse/STS plugin

To learn more detail about this report and metrics header you can follow this link Junit: Code coverage by using JaCoCo.

Conclusion

In this topic you learn about the steps to add code coverage plugin in you eclipse/STS IDE.

If this port help you to add Code Coverage plugin in you eclipse/STS IDE the like this post and share your comments. In case you are facing issue related to code coverage implementation share with us will try to resolve it.

Junit: Code coverage by using JaCoCo


Why Code Coverage Require while writing JUnit?

Code coverage is a matric indicator that shows the line of code are executed while running the automated JUnit test cases. This helps developer to identify that any unit test is not missing for developed code or newly added functionality of code so that improve the code quality and reduce number of bugs on time of integration testing.

While audit of your application source code auditor also asked for Code Coverage report so that observe your written Junit test cases covering complete application or not.

For Java development there are couple of tools for Code Coverage like Cobertura and Jacoco etc. Here you will learn about the integration of JACOCO with maven project.

JCOCO Code Coverage Plugin Integration with Eclipse/STS

Note : Cobertura support till Java version 8 only while JACOCO support all the Java 5 and above.

Code Coverage Tools generate report in percentage ratio as below in screen shot. Each header section described in detail in this table.

Junit Code Coverage

You can further extend this package section to get more code coverage detail on class and method level. You will get to know more detail in further sections.

Here these color represent as :

  • Green: Code is tested or covered
  • Red: Code is not tested or covered
  • Yellow: Code is partially tested or covered
HeaderDescription
INSTRUCTION
BRANCHAll the if-else or switch statements are consider as branch and represent as diamond in a method that can be executed or missed.
No coverage (Red diamond): No branches in the line has been executed.
Partial coverage (Yellow diamond): Only a part of the branches in the line have been executed.
CYCLOMATIC COMPLEXITYhttps://en.wikipedia.org/wiki/Cyclomatic_complexity
LINEConsider as executed when at least one instruction that is assigned to this line has been executed.
No coverage (Red): No instruction in the line has been executed.
Partial coverage (Yellow): Only a part of the instruction in the line have been executed.
Full coverage (Green): All instructions in the line have been executed
METHODConsider as executed when one of the instruction has been executed.
CLASSConsider as executed when one of the method has been executed.
JUnit Code Coverage Criteria

Implementation of Code Coverage

In this further section you will learn about the implementation of Code Coverage for Junit. Here we are using dependency as below:

  • JUnit 5
  • Maven 4
  • Maven Surefire plugin 3.0.0-M5
  • Jacoco 0.8.2 (Code Coverage library)

POM.XML

You can make following changes in your maven java application pom.xml.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.FacingIssuesOnIT</groupId>
  <artifactId>maven-junit-code-coverage-Jacoco</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Maven-junit-CodeCoverage-Jacoco</name>
  <description>Maven JUnit Code Coverage</description>
  <properties> 
     <maven.compiler.source>1.8</maven.compiler.source>
     <maven.compiler.target>1.8</maven.compiler.target>    
	<java.version>1.8</java.version>
	<!-- add property to enable or disable code coverage by default id false for Jacoco -->
	<jacoco.skip>false</jacoco.skip>
</properties>
<dependencies>
<!-- Junit 5 -->
	<dependency>
		<groupId>org.junit.jupiter</groupId>
		<artifactId>junit-jupiter-engine</artifactId>
		<version>5.5.2</version>
	</dependency>
</dependencies>
	<build>
		<plugins>
		<!-- Maven plugin for surefire with Dependency junit 5 -->
			<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M5</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>5.5.2</version>
                </dependency>
            </dependencies>
        </plugin>
        <!-- Jacoco plug in for code coverage -->
			<plugin>
				<groupId>org.jacoco</groupId>
				<artifactId>jacoco-maven-plugin</artifactId>
				<version>0.8.2</version>
				<configuration>
					<!-- exclude classes for code coverage -->
					<excludes>
						<exclude>**/*in/gov/icegate/*Application.class</exclude>
						<exclude>in/gov/icegate/controller/*</exclude>
					</excludes>
				</configuration>
				<executions>
					<execution>
						<goals>
							<goal>prepare-agent</goal>
						</goals>
					</execution>
					<!-- attached to Maven test phase -->
					<execution>
						<id>jacoco-report</id>
						<phase>test</phase>
						<goals>
							<goal>report</goal>
						</goals>
						<!-- default target/site/jacoco/* you can define your directory for code coverage report -->
						<configuration>
							<outputDirectory>target/jacoco-report</outputDirectory>
							<skip>${jacoco.skip}</skip>
						</configuration>
					</execution>
					<!-- Define rule for successful build based on code code coverage criteria -->
					<execution>
						<id>jacoco-check</id>
						<goals>
							<goal>check</goal>
						</goals>
						<configuration>
							<rules>
								<rule>
								<!-- When code coverage is 90% and no class missed then consider for successful build -->
									<element>PACKAGE</element>
									<limits>
										<limit>
											<counter>LINE</counter>
											<value>COVEREDRATIO</value>
											<minimum>0.9</minimum>
										</limit>
										<limit>
											<counter>CLASS</counter>
											<value>MISSEDCOUNT</value>
											<maximum>0</maximum>
										</limit>
									</limits>
								</rule>
							</rules>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

Here is more detail about the Jacoco code coverage implementation.

Maven Junit Code Coverage Implementation Detail

Now lets create some classes by some code implantation and check this code coverage implementation by implementing Junit.

Here i have created one Maven Java project with below directory/file structure.

Maven Junit Code Coverage Example

Implement some code as below:

Calculator.java

package com.FacingIssuesOnIT;

public class Calculator {

	   public double divide(int a, int b)
	   {
		   if(b==0)
		   {
			   throw new ArithmeticException();
		   }
		   else
		   {
		   return a/b;
		   }
	   }

	    public int multiply(int a, int b) {
	        return a * b;
	    }
	    
	    public int add(int a, int b) {
	        return a + b;
	    }
}

Address.java

package com.FacingIssuesOnIT;

public class Address {
  public String printAddress()
  {
	  return "Facing Issues On IT \n Learn from Others Experience";
  }
}

CalculatorTest.java

You can write your own Junit test cases or use in git hub by following below link. Maven JUnit Code Coverage Example

Junit Test Cases

Code Coverage report

After writing test cases you can execute same by mvn test and verify the report in folder {application path}/target/jacoco-report/index.html

It will generate report as mentioned in below screen. Here Green percentage showing code coverage for JUnit while red showing for missing Junit for code.

Maven Junit Code Coverage Report

To get in-depth detail on code coverage on class and method level you can extend by clicking package.

Maven Junit code coverage report on class level

On further extend by click on class you will het detail on method level.

Maven Junit Code Coverage report on method level

On further extend by click on particular method you will get detail about the line and branch level code coverage.

Maven JUnit Code Coverage on Line and branch level

After collecting all such information you can write junit test cases for all these missing lines/method/branch (red) of statements.

Code Coverage Rules for application build

In the above example, I have define rule for code coverage as 90% instruction execution and missing class as 0. Even If our implemented Junit executed successfully but code coverage criteria is not match with define rules then our maven install will fail and throw exception as below .

[ERROR] Failed to execute goal org.jacoco:jacoco-maven-plugin:0.8.2:check (jacoco-check) on project maven-junit-code-coverage-Jacoco: Coverage checks have not been met. See log for details. -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.

Once all the missing Junit will implement and criteria will match successful then build sucessfully.

Conclusion

In this blog you learn about the implementation of code coverage for Unit and defining rule to make build. Some times while audit of you application code quality auditor also asked about the JUnit code coverage report so that auditor can verify the written Junit covering the complete source code of your application.

References

https://www.eclemma.org/jacoco/trunk/doc/maven.html

Source Code

Download Maven Junit Code Coverage Source Code

If this example this Junit Code Coverage detail help you like post make comments. In case you are facing any issue while implementing share detail will try to help you.

[Solved]: Maven Error “Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project XYZ: There are test failures.”


While running through Run As-> Junit executing Junit test cases but when run through Maven test throwing below error.

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.916 s
[INFO] Finished at: 2021-05-08T04:50:23+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project maven-junit-code-coverage-Jacoco: There are test failures.
[ERROR] 
[ERROR] Please refer to D:\Workspace-ICEGATE-New\maven-junit-code-coverage-Jacoco\target\surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Problem

In maven java project, maven-surefire-plugin is required for any types of reports generation because mvn test command execute the test cases but there is not added the plug in for maven-surefire-plugin that’s why maven is not able to execute test cases.

Solution

To execute your test cases with Junit in maven Java project, you can add Junit dependency with maven-surefire-plugin.

JUnit 5 Dependency

<dependencies>
	<dependency>
		<groupId>org.junit.jupiter</groupId>
		<artifactId>junit-jupiter-engine</artifactId>
		<version>5.5.2</version>
	</dependency>
</dependencies>

Maven Surefire Report Plugin

<build>
		<plugins> 
			<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M5</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>5.5.2</version>
                </dependency>
            </dependencies>
        </plugin>
</plugins>
</build>

I have tested this solution with Junit 5 and working perfectly fine for me. You can change this dependency in case using Junit 4.

I have solved this problem by using above solution. If you know any other way to solve it write in comments to help others.

[Solved]: Maven Error “Source option 5 is no longer supported. Use 6 or later”


While build my maven java project getting below exception.
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project Sym360: Compilation failure: Compilation failure: 
[ERROR] Source option 5 is no longer supported. Use 6 or later.
[ERROR] Target option 1.5 is no longer supported. Use 1.6 or later.
[ERROR] -> [Help 1]

Problem

This issue is because the maven is not able to identify which version of java compiler need to use while build your code.

Solution

There are two solution to handle such problem.

  1. If you are using simple maven java application then you can add these lines in you pom.xml properties tag .
<properties> 
   <maven.compiler.source>1.8</maven.compiler.source>
   <maven.compiler.target>1.8</maven.compiler.target>
</properties>
  1. If you application is spring boot application and defining maven compiler then add these lines in your pom.xml build tag.
<build>
   <plugins>
   <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>
   </plugins>
   </build>

Based on your code written and compatibility of java version, you can update this java version like java 11 etc.

I have solved this problem by using these two solution. If you know any other way to solve it write in comments to help others.

[Solved]: IOError in Python


In Python, IOError is subclass of EnvironmentError. IOError is occurred when an input/output operation like open() file, or a method or a simple print statement is failed due to IO reason like “Disk full” or “File not found”

Example of Python IOError

In the below code, the xyz.txt file is not exist because of that the program will throw IOError exception. This IOError handle in the program through try-except block.

import sys
def readFile():
    try:
        f = open ( "xyz.txt", 'r' )
    except IOError as e:
        print (e)
readFile()

Output

[Errno 2] No such file or directory: 'xyz.txt'

This IOError exception message will print only when exception occurred.

Conclusion

In Python, IOError is a result of incorrect file name or file path. IOError occurred in multiple cases and these conditions can be handled using try except code block. Implementation way to handle IOError by try-except can check by above example.

[Solved]: AssertionError in Python


What is Assertion in Python?

Assertion is use in programming language to declare condition which should be validate as true by using assert statement prior to running the module or line of code. If assert condition is true then program control moves to next line in case it’s false the program stops running and throw AssertionError exception.

Python Syntax of Assertion

assert condition, error_message(optional)

Where to use Assertion?

In Python, Assertion can be use in following cases:

  • Checking valid input/type.
  • Checking values of parameters.
  • Detecting abuse of an interface by another programmer.
  • Checking output of a function.

Python AssertionError Example

x = 10
z = 0
# denominator can't be 0
assert z != 0, "Invalid Operation" 
print(x / z)

Output

Traceback (most recent call last):
  File "/home/xyz.py", line 4, in 
    assert z!=0, "Invalid Operation"
AssertionError: Invalid Operation

In this above Python program, The default exception handler will print the error message written by the programmer, or else will just handle the error without any message.

Solution AssertionError exception

AssertionError is subclass of Exception class, when this exception AssertionError occurs in program there are two ways to handle, either default exception handler or user handle it.

try:
    x = 10
    z = 0
    assert z != 0, "Invalid Operation"
    print(x / z)
  
#The configured error message will print in log
except AssertionError as err: 
    print(err)

Output

Invalid Operation

Conclusion

In this topic you learn about the Assertion in programming language and how to use it. In case any AssertionError occurs then you can handle it through Try-except exception handling.

[Solved]: Python SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape


In Python, This SyntaxError occurred when you are trying to access a path with normal String. As you know ‘/’ is escape character in Python that’s having different meaning by adding with different characters for example ‘\n’ is use for ne line , ‘\t’ use for tab.

This error is considered as SyntaxError because unicode forward slash (\) is not allow in path.

In further section of topic you will learn how to handle this problem in Python while writing path of file to access it.

Example of SyntaxError of Unicode Error

Lets take below example to read CSV file in Windows operating system.

import csv
with open('C:\Users\saurabh.gupta\Desktop\Python Example\input.csv','r') as csvfile:
    reader=csv.reader(csvfile)
    for record in reader:
        print(record) 

If you notice the above code is having path for windows file system to access input.csv. In windows path mentioned by using forward slash (\) while in Python programming forward slash(\) is use for handling unicode characters. That’s why when you execute the above program will throw below exception.

Output

File "C:/Users/saurabh.gupta14/Desktop/Python Example/ReadingCSV.py", line 2
    with open('C:\Users\saurabh.gupta\Desktop\Python Example\input.csv','r') as csvfile:

Solution

The above error is occurred because of handling forward slash(\) as normal string. To handle such problem in Python , there are couple of solutions:

1: Just put r in path before your normal string it converts normal string to raw string:

with open(r'C:\Users\saurabh.gupta\Desktop\Python Example\input.csv','r') 


2: Use back slash (/) instated of forward slash(\)

with open('C:/Users/saurabh.gupta/Desktop/Python Example/input.csv','r') 

3: Use double forward slash (\\) instead of forward slash(\) because in Python the unicode double forward value convert in string as forward slash(\)’

with open('C:\\Users\\saurabh.gupta\\Desktop\\Python Example\\input.csv','r') 

If this solution help you , Please like and write in comment section or any other way you know to handle this issue write in comment so that help others.

[Solved]: StopIteration in Python


In Python, StopIteration is an exception which occurred by built-in next() and __next__() method in iterator to signal that iteration is done for all items and no more to left to iterate.

Example of StopIteration

In this example string value “FacingIssuesOnIT” is Iterating to print character. In this case while loop will run indefinitely and call next() method on iterable value to print value.

iterable_value = 'FacingIssuesOnIT'
iterable_obj = iter(iterable_value)
 
while True:
    try: 
        # Iterate by calling next
        item = next(iterable_obj)
        print(item)
    except StopIteration as err:
        print('Stop Iteration occured')
        break

Output

F
a
c
i
n
g
I
s
s
u
e
s
O
n
I
T
Stop Iteration occurred

In this program after completing the iteration next() element print of iterable_value when it goes to next element print it will throw StopIteration exception because there is no more element in iterable_value.

Solution

Whenever you apply the next() method of iterable object always check the length of iterable object then run the loop to get element by next() method.

[Solved]: AttributeError in Python


In Python, AttributeError is sub class of Exception. AttributeError occurred when an attribute reference or assignment fails because pointing object does not support attribute references or attribute assignment.

Example AttributeError

In this Python program, Test class is having attribute p and initialize with 0 inside the constructor __init__().

class Test:
    def __init__(self):
        self.p = 0
f = Test()
print(f.p)
print(f.q)

Output

    print(f.q)

AttributeError: 'Test' object has no attribute 'q'

This Python program is throwing AttributeError in line 6 because attribute q is not declared inside the class Test but trying to print through program that’s why Python throw AttributeError.

Solution

To solve this problem you can handle exception in program by using try and except block.

try:
    class Test:
        def __init__(self):
            self.p = 0
    f = Test()
    print(f.p)
    print(f.q)
except Exception as e:
    print (e)
    print ('This is an example of StandardError exception')

Output

'Test' object has no attribute 'q'
This is an example of AttributeError exception

Conclusion

In this topic you learn about the case when the AttributeError occurred in Python program and way to handle this error through exception handling.

[Solved]: Python FloatingPointError


In Python, FloatingPointError is subclass of ArithmeticError. FloatingPointError occured with floating point operations when floating point exception control (fpectl) is turned on.
To Enable fpectl in Python requires an interpreter compiled with the –with-fpectl flag.

Note : In Python, fpectl module is not built by default, you have explicitly import it to control over the floating point units from several hardware manufacturer. This allow the use to turn on the generation of SIGFPE whenever any IEEE-754 exceptions Division by Zero, Overflow, or Invalid Operation occurs.

Example FloatingPointError

The below example is created to occurs FloatingPointError in Python program where just trying exponential of number with certain values before and after enabling the fpectl.

import sys
import math
import fpectl
try:
    print ('FPECTL Control Not Enable:', math.exp(750))
    fpectl.turnon_sigfpe()
    print ('FPECTL Control Enabled:', math.exp(750))
except Exception as err:
    print (err)
    print (sys.exc_type)

Output

FPECTL Control Not Enable: 1.01423205474e+304
FPECTL Control Enabled: in math_1
<type 'exceptions.FloatingPointError'>

Solution

When dealing with any floating point number and performing operation where precision values can be more always handle Exception.

To learn more on exception handling follow the link Python: Exception Handling.

If this blog for solving FloatingPointError help you to resolve problem make comment or if you know other way to handle this problem write in comment so that it will help others.

[Solved]: python OverflowError : (34, ‘Result too large’)


In Python, OverflowError is subclass of ArithmeticError. This error occurred for floating points numbers when size exceed the limit of variable type.

In case of Integer when size grow variable convert to long value. If variable size exceed the limit of Long also then Python throw MemoryError.

Example of OverFlowError

In this Python program we are just continually multiplying the value for floating numbers with 2 as long as for loop condition match (50 times) because it’s floating number and having limit of size once this size limit will exceed will throw exception as OverflowError.

i=1
try:
    f = (2.0**i)
    for i in range(50):
        print (i, f)
        f = f ** 2
except OverflowError as err:
    print ('Overflowed on power ', f, err)

Output

0 2.0
1 4.0
2 16.0
3 256.0
4 65536.0
5 4294967296.0
6 1.8446744073709552e+19
7 3.402823669209385e+38
8 1.157920892373162e+77
9 1.3407807929942597e+154
Overflowed on power  1.3407807929942597e+154 (34, 'Result too large')

If you noticed this Python program this is throwing error as “OverflowError: (34, ‘Result too large’)” because floating variable size is continuesly increasing once it will reach to 34 for precision will throw OverflowError.

Solution

In Python, when you are performing operation of floating number and these operations are inside the recursive method or loops then always handle the OverflowError by try catch block so that your program will not terminate.

[Solved]: Python NameError: name ‘XYZ’ is not defined


In Python, NameError occurred for identifier when it’s being used but not defined in local or global scope so Python will able to find and throw exception. NameError can be occurred by following reasons:

1: Misspelled built-in function
2: Using unidentified variables
3: Define variable after used
4: Incorrect Usage of Scope

Note : In any programming language Identifier is name of variable, function, class or object.

We will discuss in detail about all these above cases with example.

Example : Misspelled built-in function

In this example by mistake not type correct name for print function. I have written as ‘prin’ instated of ‘print’ that’s why Python with throw NameError.

sugar_weight = input("Enter how much sugar you want: ")
prin(sugar_weight)

Output

   prin(sugar_weight)

NameError: name 'prin' is not defined

Example : Using unidentified variables

In this example calculating the sub of variable ‘A’ and ‘B’ but variable B is not defined that’s why Python with throw NameError.

A=5
sum=A + B
print("Sum:"+sum)

Output

   sum=A + B

NameError: name 'B' is not defined

Example : Define variable after used

In this example the variable sum is getting printed before going declare or assigned that’s why Python with throw NameError.

A=5
B=10
print("Sum:"+add)
add=A + B

Output

 print("Sum:"+add)

NameError: name 'add' is not defined

Example : Incorrect Usage of Scope

In this example the avg variable is having local scope with in the function calculateAverage but trying to access for print outside the function. In this case This avg variable will be undefined out the calculateAverage() method then Python will throw NameException.

def calculateAverage(numbers):
    avge = sum(numbers)/len(numbers)

numbers=[10,20,30,40,50] 
calculateAverage(numbers)
print(avge)

Output

  print(avge)

NameError: name 'avge' is not defined

Solution for NameError

In Python NameError related to variable or function name must be fix by modifying the name of variable or correct the code. In case NameError can occurred on runtime then you can explicitly use try and except block to handle NameError.

def calculateAverage(numbers):
    avge = sum(numbers)/len(numbers)
try:
	numbers=[10,20,30,40,50] 
	calculateAverage(numbers)
	print(avge)
except NameError:
    print ("NameError occured in code because some variable isn't defined.")

Output

NameError occured in code because some variable isn't defined.

Conclusion

In this topic you learn about the different cases where NameError can be occurred. It’s explained with example for NameError and finally provided solution to handle NameError by exception handling.

To learn more on exception handling follow the link Python: Exception Handling.

If this blog for solving NameError help you to resolve problem make comment or if you know other way to handle this problem write in comment so that it will help others.

[Solved] : TypeError: ‘builtin_function_or_method’ object is not iterable


In Python, When in built-in function used it must be specify with parenthesis (()) after the name of the function. If you try to run or iterate the program over a built-in method or function without parenthesis (()) the Python will throw exception as “TypeError: builtin_function_or_method is not iterable”.

Exampe TypeError: builtin_function_or_method is not iterable

Let’s consider the scenario of successful execution of a built in function in Python.

fruits = ["Papaya", "Orange", "Grapes", "Watermelon", "Apple"]
print(", ".join(fruits))

In Python, The join() is a built-in function which turns a list into a string and adds a separator between each value in a string. The output of code as below.

Output

Papaya, Orange, Grapes, Watermelon, Apple

In case while writing code, you forget the brackets (()) in built-in function then Python will throw an error. In this below scenerio will throw exception as “TypeError: builtin_function_or_method is not iterable

user = {
    "name": "Saurabh Gupta",
    "age": 35,
    "city": "Noida"
}
#iterate user dictionary
for key, value in user.items:
    print("Key:", key)
    print("Value:", str(value))

Output

File "C:/Users/saurabh.gupta/Desktop/Python Example/test.py", line 7, in <module>
    for key, value in user.items:

TypeError: 'builtin_function_or_method' object is not iterable

The above example is throwing as “TypeError: ‘builtin_function_or_method‘ object is not iterable” because while using items function of dictionary programmer missed to write parenthesis (()) because for loop is iteration operation and it’s required Iterable object but items method is used without parenthesis that’s why Python is considering as object and throwing exception as “TypeError: ‘builtin_function_or_method’ object is not iterable“.

Solution

To resolve such problem related to built-in function or any function always write method with parenthesis (()).

You make correct the above program by writing items method with parenthesis as below in line no 7

user = {
    "name": "Saurabh Gupta",
    "age": 35,
    "city": "Noida"
}
#iterate user dictionary
for key, value in user.items():
    print("Key:", key)
    print("Value:", str(value))

Output

Key: name
Value: Saurabh Gupta
Key: age
Value: 35
Key: city
Value: Noida

Conclusion

This type of exception “TypeError: builtin_function_or_method is not iterable” is common when user forget to use parenthesis (()) while using built-in function.

To learn more on exception handling follow the link Python: Exception Handling.

If this blog for solving TypeError help you to resolve problem make comment or if you know other way to handle this problem write in comment so that it will help others.

[Solved]: ValueError: invalid literal for int() with base 10


Python supports explicit type conversion by converting values to different data types. In Python you can convert integers to strings, strings to integers, floats to integers but one conversion Python does not support a float as a string to an integer. In if you try to convert from float as an string to integer then it throw exception as “ValueError: invalid literal for int() with base 10“.

Example: ValueError: invalid literal for int() with base 10

Let’s take a below example where asking weight as input from user, as you know weight can be float value. In this example if user input weight more than 10 KG then user will get discount of 100 Rs. otherwise not discount.

sugar_weight = input("Enter how much sugar you want: ")

sugar_weight_as_int = int(sugar_weight)

if sugar_weight_as_int > 10:
    print("You have discount of 100/- Rs.")
else:
    print("You do not have discount on sugar.")

Output

Enter how much sugar you want: 6.5
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    sugar_weight_as_int = int(sugar_weight)
ValueError: invalid literal for int() with base 10: '6.5'

In this example throw exception as “ValueError: invalid literal for int() with base 10: ‘6.5’” because here user insert the sugar weight as 6.5 which is float string value now to comparing with integer value type casting as int but Python doesn’t support type casting from float string to integer that’s why Python will throw exception as “ValueError: invalid literal for int() with base 10: ‘6.5’“.

If you noticed the above error messages have two parts:

1: ValueError: This error occured when there is an issue with the value stored in a particular object.
2: Error Message “invalid literal for int() with base 10: ‘6.5’” : This means the value we have passed through an int() method cannot be converted because the int() method does not allow you to pass a float represented as a string.

Solution

To resolve this issue, you should convert this passing float string value to float then this exception will get resolve because Python allows float string to float conversion.

sugar_weight = input("Enter how much sugar you want: ")

sugar_weight_as_int = int(float(sugar_weight))

if sugar_weight_as_int > 10:
    print("You have discount of 100/- Rs.")
else:
    print("You do not have discount on sugar.")

Output

Enter how much sugar you want: 6.5
You do not have discount on sugar.

Conclusion

In Python “ValueError: invalid literal for int() with base 10” error is occurred when you try to convert a string value that is not formatted as an integer. To overcome this issue you can use the float() method to convert a floating-point number in a string to an integer. Then, you can use int() to convert your number to an integer.

If this solution does not work, make sure the input value of a string does not contain any letters because Strings with letters cannot be converted to an integer unless those letters have a special meaning.

To learn more on exception handling follow the link Python: Exception Handling.

If this blog for solving ValueError help you to resolve problem make comment or if you know other way to handle this problem write in comment so that it will help others.

[Solved]: IndexError: tuple index out of range


In Python, lists, tuples are indexed. It means each value in a tuple is associated with index position (0 to n-1) to access that value. Where N represent the total number of values in list or tuple. When user try access an item in a tuple that is out of range the Python returns an error that says “IndexError: tuple index out of range”.

Example: IndexError: tuple index out of range

Lets consider a below example for tuple of fruits. Where index of value start from 0 and up to (number of element -1).

fruits = ("Apple", "Banana", "Grapes", "Papaya", "Litchi")

This tuple fruits is having five values and each element is associated with index number as below:

AppleBananaGrapesPapayaLitchi
01234

To access the value “Grapes” from fruits tuple, we would use this code:

print(fruits[2])

Our code returns: Grapes. here we accessing the value at the index position 2 and print it to the console. Same way we can try with other values in tuple.

Now lets consider an example to create this IndexError, Try to access value by using index value out of range (3 to 6) where index position 5 is out of range and this example will throw exception as “IndexError: tuple index out of range“.

fruits = ("Apple", "Banana", "Grapes", "Papaya", "Litchi")
for i in range(2, 6):
    print(fruits[i])

Output

Grapes
papaya
Lichi

Traceback (most recent call last):
  File "main.py", line 4, in <module>
    print(fruits[i])
IndexError: tuple index out of range

Our code prints out the values Grapes, Papaya and Litchi. These are the last three values in our tuple. Then throw exception as “IndexError: tuple index out of range” because index position 5 is out of the range for elements in the tuple.

The Solution

Our range() statement creates a list of numbers between the range of 2 and 6. This number list is inclusive of 2 and exclusive of 6. Our fruits tuble is only indexed up to 4. This means that our loop range will try to access a fruit at the index position 5 in our tuple because 5 is in our range.

Now lets try to run this below updated program for loop range 2 to 5 then observe the result. To learn more on for loop follow link Python: for loop

fruits = ("Apple", "Banana", "Grapes", "Papaya", "Litchi")
for i in range(2, 5):
    print(fruits[i])

Output

Grapes
papaya
Litchi

Our code successfully prints out the last three items in our list because now accessing items at the index positions 2, 3, and 4 which is in range of fruit tuple indexes.

Conclusion

The IndexError: tuple index out of range error occurs when you try to access an item in a tuple that does not exist. To solve this problem, make sure that whenever you access an item from a tuple that the item for which you are looking exists.

To learn more on exception handling follow the link Python: Exception Handling.

If this blog for solving IndexError help you to resolve problem make comment or if you know other way to handle this problem write in comment so that it will help others.

[Solved]: TypeError: ‘int’ object is not subscriptable


In Python some objects are subscriptable. It means that they contain, or can contain, other objects. Integers are used to store whole numbers so that are not a subscriptable object. If programmer treat an integer like a subscriptable object, an error will be raised like “TypeError : ‘int’ object is not subscriptable“.

If you noticed this is TypeError and it occurs when you try to perform operation that’s doesn’t support on object for example when you concatenate string and int then it will throw TypeError . You can more detail on [Solved] TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’.

Example : TypeError: ‘int’ object is not subscriptable

In this example, user is inserting date of birth in the format of DDMMYYYY then parse this date in Date (DD), Month(MM) and Year(YYYY).

dob = int(input("When is your date of birth? (ddmmyyyy) "))

day = dob[0:2]
month = dob[2:4]
year = dob[4:8]

print("Day:", day)
print("Month:", month)
print("Year:", year)

Output

When is your date of birth? (ddmmyyyy) 19051987
Traceback (most recent call last):

File “”, line 1, in
runfile(‘C:/Users/saurabh.gupta/Desktop/Python Example/Exception Test.py’, wdir=’C:/Users/saurabh.gupta/Desktop/Python Example’)

File “C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py”, line 705, in runfile
execfile(filename, namespace)

File “C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py”, line 102, in execfile
exec(compile(f.read(), filename, ‘exec’), namespace)

File “C:/Users/saurabh.gupta/Desktop/Python Example/Exception Test.py”, line 30, in
day = dob[0:2]

TypeError: ‘int’ object is not subscriptable

In this above example, user input date of birth (19051987) and convert this date to int. Which is now whole number and not subscriptable. now in code try to parse this date of birth in form of Day (DD), Month (MM) and Year (YYYY). Because it’s whole integer number when you try to parse this integer value will through exception as “TypeError: ‘int’ object is not subscriptable

Solution

In this above program issue is because programmer is explicitly type casting the string date of birth to integer which is whole number and not subscriptable to resolve this problem programmer need to remove this explicit integer type casting as below.

dob = input("When is your date of birth? (ddmmyyyy) ")

day = dob[0:2]
month = dob[2:4]
year = dob[4:8]

print("Day:", day)
print("Month:", month)
print("Year:", year)

Output

When is your date of birth? (ddmmyyyy) 19051987
Day: 19
Month: 05
Year: 1987

To learn more on exception handling follow the link Python: Exception Handling.

If this blog for solving KeyError help you to resolve problem make comment or if you know other way to handle this problem write in comment so that it will help others.

[Solved]: KeyError: XYZ in Python


In Python, KeyError occurred when try access a value in dictionary by key name but key don’t exist. If key found in dictionary will return a value if doesn’t exist then through KeyError : key_name .

How to handle KeyError in Python?

You can handle KeyError while accessing the key from dictionary by following ways:

  • Check for key in advance for accessing the key
  • Use the ‘in’ keyword to check for key
  • Use try and except block.

Example of KeyError in Python

Lets take simple example where user want to access key from dictionary to retrieve value. In this example if user input key name, age or city either of them then it will return value. if user input other value as key that doesn’t exist in dictionary then it will throw exception as KeyError: passing_key.

user = {
    "name": "Saurabh Gupta",
    "age": 35,
    "city": "Noida"
}

key_name=input("What information you want to get? (name, age, city)")
print(key_name+" :"+user[key_name])

Now as suggested above solution to KeyError, lets fix the above to problem to exception handle and show message in case user input other keys except in dictionary.

Solution 1 : Iterate all key and value

Lets take first solution to get keys from dictionary then retrieve values from dictionary.

user = {
    "name": "Saurabh Gupta",
    "age": 35,
    "city": "Noida"
}
#iterate user dictionary
for key, value in user.items():
    print("Key:", key)
    print("Value:", str(value))

In this solution user will able to print all values w.r.t each key.

Solution 2 : Advance check by in

Lets take second solution to fix this problem to check key in dictionary first by using ‘in’ in if statement. if key not available in dictionary then print else statement.

user = {
    "name": "Saurabh Gupta",
    "age": 35,
    "city": "Noida"
}

key_name=input("What information you want to get? (name, age, city)")
if key_name in user:
    print(key_name+" :"+user[key_name])
else:
    print(key_name +" key is not available in user")

in this solution if user input any key like name, age or city then print value otherwise print as key is not available.

Solution 3 : try and except

Lets take third solution to solve KeyError by using exception handling through try and except.

user = {
    "name": "Saurabh Gupta",
    "age": 35,
    "city": "Noida"
}

key_name=input("What information you want to get? (name, age, city)")
try:
    print(key_name+" :"+user[key_name])
except KeyError:
    print(key_name +" key is not available in user")

In this solution if key is correct then print value w.r.t key in dictionary. if key doesn’t exist and KeyError exception occurs the print statement as “Key is not available in user”.

To learn more on exception handling follow the link Python: Exception Handling.

If this blog for solving KeyError help you to resolve problem make comment or if you know other way to handle this problem write in comment so that it will help others.

[Solved]: SyntaxError in Python


What is Syntax in programming Language?

In any programming language, Syntax are set of rules which dictate how program need to written. Every language have different syntax from other language like C, C++, Java, C# having curely bracket ({}) to represent block of statements while in Python blocks like if-else, loops, functions etc. represent by indentations. We can also think of example of our spoken language having grammar, punctuation etc.

You can identify these exceptions on runtime when you code give SyntaxError the you can fix it. In Python, Sometime programmer omit some set statement, not following indentation or programmer move from another language put curly bracket({}),semi colon (:) etc. then it show SyntaxError because python doesn’t support that.

In this blog you will learn about the some of the common Python SyntaxErrors, provide examples, and show you how to fix them.

Example 1:

In this below example where calculating average of numbers and returning final result to print but this simple example will through exception as in output

numbers = [8, 7, 9, 8, 7]

def calculate_average_age():
    average = sum(numbers) / len(numbers)
    print(average)
return average

total_average = calculate_average_age(numbers)
print(total_average)

Output

File “C:/Users/saurabh.gupta/Desktop/Python Example/Exception Test.py”, line 12
return average
^
SyntaxError: ‘return’ outside function

Solution

The above issue is because of indentation of return statement that’s why it’s showing in error as ‘return is outside the function’. To fix this problem return should be same intended as other statement of function as below.

numbers = [8, 7, 9, 8, 7]

def calculate_average_age():
    average = sum(numbers) / len(numbers)
    print(average)
    return average
total_average = calculate_average_age(numbers)
print(total_average)

Example 2

Let’s take another example of dictionary which will print the elements as key and value but this program will through exception as “SyntaxError: Invalid syntax“.

user = {
    "name": "Saurabh Gupta",
    "age": 35
    "city": "Noida"
}
#iterate user dictionary
for key, value in user.items():
    print("Key:", key)
    print("Value:", str(value))

Output

File “C:/Users/saurabh.gupta/Desktop/Python Example/Exception Test.py”, line 4
“city”: “Noida”
^
SyntaxError: invalid syntax

Solution

In this above example, if you noticed this exception occurred in the line 4 inside the dictionary. It’s because of missing commas in each element for key and value that’s why it’s showing exception as “SyntaxError: invalid syntax“.

You can modify the above code after adding comma in dictionary as below:

user = {
    "name": "Saurabh Gupta",
    "age": 35,
    "city": "Noida"
}
#iterate user dictionary
for key, value in user.items():
    print("Key:", key)
    print("Value:", str(value))

Output

The above line of updated code will return output as below:

Key: name
Value: Saurabh Gupta
Key: age
Value: 35
Key: city
Value: Noida

Summary

In this topic you learn about the Syntax inside the programming language and in case any SyntaxError happen in your code you can identify the problem by checking he stack trace of logs by line number then identify the problem accordingly fix it.

Learn Python exception handling in more detain in topic Python: Exception Handling

[Solved] ValueError: math domain error


In Python, some of the mathematics operation doesn’t support negative or zero values. In this case when you perform operation with negative value then on runtime Python throws exception as “ValueError: Mathematics Domain Error” .

Here are some operations like sqrt() , log() etc. method which doesn’t work with negative values.

Example:

In this below example, we will try to create scenario by passing both positive and negative numbers and see the results.

import math

number = input("Please insert a number: ")

square=math.sqrt(int(number))

print("Square of number "+str(number) +" is :"+str(square))

Output

Passing Positive Number : 5

Please insert a number: 5
Square of number 5 is :2.23606797749979

Passing Negative Number: -5

File “C:/Users/saurabh.gupta/Desktop/Python Example/Exception Test.py”, line 5, in
square=math.sqrt(int(number))

ValueError: math domain error

After passing the value as -5 it’s throwing the runtime exception as “ValueError:math domain error“.

Solution

The solution to handle such situation is apply condition which check for negative values in case any negative values pass by user the show message to user as this operation is not allow for negative values.

import math

number = input("Please insert a number: ")
if(int(number)>0):
    square=math.sqrt(int(number))
    print("Square of number "+str(number) +" is :"+str(square))
else:
    print("Negative values are not allow for SQRT() operation")

Output

Passing Positive Number : 5

Please insert a number: 5
Square of number 5 is :2.23606797749979

Passing Negative Number : -5

Please insert a number: -5
Negative values are not allow for SQRT() operation

If you noticed from above program in solution, I just put one condition to check the value of input number to resolve the problem “ValueError: math domain error

[Solved] ValueError: too many values to unpack (Expected X)


In Python, When bags are not unpack well then it throws exception as “ValueError: Too many values to unpack (Expected X) . This exception generally occurs in two cases:

  • When you try to iterate over a dictionary and unpack its keys and values separately.
  • When you forget to unpack every item from a list to a variable.

If you noticed this is ValueError and it’s occurred when you try to access value that doesn’t exists. Values can be any type like object, string, list or dictionary.

Example 1

In this below dictionary iteration example, print values of key and value from user dictionary but it will through exception as “ValueError: Too many values to unpack (expected 2)“. Its’ because loop is try to access two values key and value from dictionary while there is only value because dictionary consider each as value.

user = {
	"name": "Saurabh Gupta",
	"age": 35,
	"city": "Noida"
}
#iterate user dictionary
for key, value in user:
	print("Key:", key)
	print("Value:", str(value))

Output

File “C:/Users/saurabh.gupta/Desktop/Python Example/Exception Test.py”, line 13, in
for key, value in user:

ValueError: too many values to unpack (expected 2)

Solution

To resolve above problem and access key and value from dictionary each item , you can use dictionary items() method which analyze keys and values from dictionary and return these values in iteration. You can modify the code as below:

user = {
	"name": "Saurabh Gupta",
	"age": 35,
	"city": "Noida"
}
#iterate user dictionary
for key, value in user.items():
	print("Key:", key)
	print("Value:", str(value))

Output

After adding items() method in code you can observe below output which is returning key and value of dictionary.

Key: name
Value: Saurabh Gupta
Key: age
Value: 35
Key: city
Value: Noida

Note: Till Python 2.0 use method iteritems() in Python 3+ it’s replace with method items().

Example 2

In Python, allows sequence of values to assign on number of variables. In case number of values are more then variables, it throws exception as “ValueError: too many values to unpack (expected X)

grapes, bananas, apples, papaya = [250, 80, 200, 300, 500]

Output

It will throws exception as below because count of variables are less then the values on list.

File “C:/Users/saurabh.gupta/Desktop/Python Example/Exception Test.py”, line 11, in
grapes, bananas, apples, papaya = [250, 80, 200, 300, 500]

ValueError: too many values to unpack (expected 4)

In case number of values are less then variables, it throws exception as “ValueError: not enough values to unpack (expected X)”

grapes, bananas, apples, papaya = [250, 80, 200]

Output :

File “C:/Users/saurabh.gupta/Desktop/Python Example/Exception Test.py”, line 18, in
grapes, bananas, apples, papaya = [250, 80, 200]

ValueError: not enough values to unpack (expected 4, got 3)

This is because of number of values are less (3) while number of values

To resolve such type of issues, always make sure the number of variables should be equal to number of values in list.

[Solved]: TypeError: cannot unpack non-iterable NoneType object


In Python, TypeError is subclass of Exception. Python sequence can be unpacked. This means you can assign content of sequence to multiple variables. If you try to assign a None value to a variable by using this syntax then it throws error as “TypeError: Can not unpack Non-iterable None Type object”.

Note: Syntax error should not be handle through exception handling it should be fixed in your code.

You can check complete list of built-in exception hierarchy by following link. Python: Built-in Exceptions Hierarchy

How to unpack sequence elements to variables?

In this below unpacking sequence the elements of list will assign to variables in sequence. For example:

fruit_prices = [250, 80, 200]
grapes, bananas, apples = fruit_price

In the above code the values in fruit_prices will assign in variables as below :

grapes=250, bananas=80, apples=200

Lets take another example of unpacking sequence from function where return values from functions can be assigned in sequence of variables. For Example:

prices = [4.30, 5.90, 6.70, 3.90, 5.60, 8.30, 6.50]
def calculate_statistics(prices):
    average_price = sum(prices) / len(prices)
    largest_price = max(prices)
    return average_price, largest_price
average, largest = calculate_statistics(prices)
print("Average :"+str(average))
print("Largest :"+str(largest))

In this above example, If you will see it’s returning two values (line 5) from calculate_statistics function and returned values will assign to variables average and largest in sequence (line 6).

Average : 5.88

Largest : 8.30

Scenario for Exception

Now lets create scenario for creating exception, I have modified the above code with comments the line # 5. It will display the code as below

prices = [4.30, 5.90, 6.70, 3.90, 5.60, 8.30, 6.50]
def calculate_statistics(prices):
    average_price = sum(prices) / len(prices)
    largest_price = max(prices)
    return average_price, largest_price
average, largest = calculate_statistics(prices)
print("Average :"+str(average))
print("Largest :"+str(largest))

When you execute the above code will through exception as below

average, largest = calculate_statistics(prices)

TypeError: ‘NoneType’ object is not iterable

In this example, Hope you understand the scenario as the number of values in sequence will assigned to value on same number of value.

Learn Python exception handling in more detain in topic Python: Exception Handling

[Solved] : TabError: inconsistent use of tabs and spaces in indentation


In Python, TabError is sub class of IndentationError. Python allows code style by using indentation by space or tabs. If you are using both while writing code for indentation then Python encounter “TabError : inconsistent use of tabs and spaces in indentation”.

In Python, Indentation is important because the language doesn’t depend on syntax like curly brackets to denote where a block of code starts and finishes . Indents tell Python what lines of code are part of what code blocks.

Note: Syntax error should not be handle through exception handling it should be fixed in your code.

You can see complete Python exception hierarchy through this link : Python: Built-in Exceptions Hierarchy.

Example

Consider a below scenario where indentation is use by implementing space and tab both on line 3 (used space for indentation) while in line 4 (used tabs for indentation). When you will run the below program it will throw exception as mentioned in output.

numbers = [3.50, 4.90, 6.60, 3.40]
def calculate_total(purchases):
	total = sum(numbers)
        return total
total_numbers = calculate_total(numbers)
print(total_numbers)

Output

File “C:/Users/saurabh.gupta/Desktop/Python Example/Exception Test.py”, line 10
return total
^
TabError: inconsistent use of tabs and spaces in indentation

Solution

To resolve this issue, you have done some minor change in your code for indentation by either space or tabs and run the program will work fine.

numbers = [3.50, 4.90, 6.60, 3.40]
def calculate_total(purchases):
    total = sum(numbers)
    return total
total_numbers = calculate_total(numbers)
print(total_numbers)

Output

18.4

Learn Python exception handling in more detain in topic Python: Exception Handling

[Solved] : IndentationError: unexpected indent in Python


Base class of IndentationError is SyntaxError. This exception occurred in Python because of incorrect Indentation because Python don’t use curly brackets for segregate blocks for loop, if-else, functions etc. it’s identify the blocks based on indentation only. Sometime if with in same block there is difference in indentations then it can throw TabError.

Note: Syntax error should not be handle through exception handling it should be fixed in your code.

You can check complete list of built-in exception hierarchy by following link. Python: Built-in Exceptions Hierarchy

Example

Here is simple example of reading the csv file by Python csv module. It’s throwing indentation error because of not proper indentation in second statement.

import csv
   with open(r'C:\Users\saurabh.gupta14\Desktop\Python Example\input.csv','r') as csvfile:
    reader=csv.reader(csvfile)
    for record in reader:
        print(record)

Output

File “C:/Users/saurabh.gupta14/Desktop/Python Example/ReadingCSV.py”, line 2
with open(‘C:\Users\saurabh.gupta14\Desktop\Python Example’,’r’) as csvfile:
^
IndentationError: unexpected indent

Solution

In the above example the second line is start from after taking tab which is not required. It should start without taking any space or tab. To fixed this issue i have remove the space and run it again.

import csv
with open(r'C:\Users\saurabh.gupta14\Desktop\Python Example\input.csv','r') as csvfile:
    reader=csv.reader(csvfile)
    for record in reader:
        print(record) 

The above modified code with not throw the IndentationError.

Learn Python exception handling in more detain in topic Python: Exception Handling

[Solved] ZeroDivisionError: division by zero in Python


The super class of ZeroDivisionError is ArithmeticError. This exception raised when the second argument of a division or modulo operation is zero. The associated value is a string indicating the type of the operands and the operation.

In simple term in any arithmetic operation when value divided by zero then in Python throw ZeroDivisionError.

You can see complete Python exception hierarchy through this link : Python: Built-in Exceptions Hierarchy.

Example :

In the Python program will throw ZeroDivisionError in case of num_list is not having any element then it’s length become 0 and while executing this program will through ZeroDivisionError.

num_list=[]
total=0
avg=total/len(num_list)
print("Average:"+avg)

Output

ZeroDivisionError : Division by Zero

Solution

While implementing any program logic and there is division operation make sure always handle ArithmeticError or ZeroDivisionError so that program will not terminate. To solve above problem follow this example:

num_list=[]
total=0
try:
    avg=total/len(num_list)
    print("Average:"+avg)
except ZeroDivisionError:
    print ("Zero Division Error occurred")

Output

Zero Division Error occurred.

In this above modified code applied exception handling for particular code section so that program will not terminate.

Learn Python exception handling in more detain in topic Python: Exception Handling

Python: Exception Handling


What is an Exception?

In Python, Sometimes the programs may misbehave or terminate/crash unexpectedly due to some unexpected events during the execution of a program. These unexpected events are called as exceptions and the process of handling them to avoid misbehavior or crashing the program is called as exception handling.

Let’s execute the below code in python and have a look at the output.

	def calculate_expenditure(list_of_expenditure):
	    total=0
	    for expenditure in list_of_expenditure:
	        total+=expenditure
	    print(total)
	list_of_values=[100,200,300,"600",500]
	calculate_expenditure(list_of_values)

Above code will give an error, one way to take care of such error situation is to use selection constructs. The error was due to addition of a string (“600”) to an integer. If we add a condition to check whether the expenditure is of type int, that would solve this error.

But that can cause further issues. Let’s see that by executing the below code in python playground.

	def calculate_expenditure(list_of_expenditure):
	    total=0
	    for expenditure in list_of_expenditure:
	        if(type(expenditure) is int):
	            total+=expenditure
	        else:
	            print("Wrong data type")
	            break
	    print(total)
	
	list_of_values=[100,200,300,"600",500]
	calculate_expenditure(list_of_values)

Although we have handled this error using if statement, the function itself returns wrong output when there is error in the input. 
The ideal situation would be if the function can tell us that something went wrong.

try and except

In python we can create a try and except block of code to handle exceptions.
If any exception occurs in the try block of code, it will jump to except block of code. Once the except block is executed, the code continues to execute other statements outside except block.

	def calculate_expenditure(list_of_expenditure):
	    total=0
	    try:
	        for expenditure in list_of_expenditure:
	            total+=expenditure
	        print(total)
	    except:
	        print("Some error occured")
	    print("Returning back from function.")
	
	list_of_values=[100,200,300,"600",500]
	calculate_expenditure(list_of_values)

With this we will not get incorrect output like before.

Built-in Exceptions

Python has many kinds of exceptions predefined as part of the language. BaseException is the base class of all type of exception. Below are some most common types built-in exception.

You can check complete list of built-in exception hierarchy by following link. Python: Built-in Exceptions Hierarchy

Built-in exceptionWhen it will raiseExample
ZeroDivisionErrorWhen a value is divided by zero.num_list=[]
total=0
avg=total/len(num_list)
TypeErrorWhen we try to do an operation with incompatible type.total=10
total+=”20″
NameErrorWhen try to access a variable which is not defined.avg=total/10 #where total is not defined
IndexErrorWhen try to access a index value which is out of range.num_list=[1,2,3,4]
value=num_list[4]
ValueErrorWhen we use a valid data type for an argument of a built-in function but passes an invalid value for itstring is a valid data type for int() but the value “A” is invalid, as “A” can’t be converted into int.
value=”A”
num=int(value)

Python also allows us to handle different exceptions that can occur separately. That means you can have a different action or message for every unique exception that occurs.

Exception Handling Example

Here is the same expenditure calculation code with additional average expenditure calculation.

	def calculate_expenditure(list_of_expenditure):
	    total=0
	    try:
	        for expenditure in list_of_expenditure:
	            total+=expenditure
	        print("Total:",total)
	        avg=total/num_values
	        print("Average:",avg)	    
        except ZeroDivisionError:
	        print("Divide by Zero error")
	    except TypeError:
	        print("Wrong data type")
	    except:
	        print("Some error occured")
	list_of_values=[100,200,300,"400",500]
	num_values=0
	calculate_expenditure(list_of_values)

Note:

  1. Default except block is the one without any type mentioned.
  2. If an error occurs and the matching except block is found, then that is executed.
  3. If an error occurs and the matching except block is not found, it executes the default except block.
  4. If an error occurs and the matching except block is not found and if the default except block is also not found, the code crashes.
  5. The default except block, if present should be the last except block, otherwise it will result in a runtime error.

Exception handling inside a function

If an exception occurs inside a function and if the exception is not caught inside it, then the exception is transferred to the function call. We have another opportunity to catch it, if we write function call inside another try and except block.

Try the below code in python playground and observe the output.

	def calculate_sum(list_of_expenditure):
	    total=0
	    try:
	        for expenditure in list_of_expenditure:
	            total+=expenditure
	        print("Total:",total)
	        avg=total/no_values
	        print("Average:",avg)
	    except ZeroDivisionError:
	        print("Divide by Zero error")
	    except TypeError:
	        print("Wrong data type")
	
	try:
	    list_of_values=[100,200,300,400,500]
	    num_values=len(list_of_values)
	    calculate_sum(list_of_values)
	except NameError:
	    print("Name error occured")
	except:
	    print("Some error occured")

finally

Sometimes in programming we need to execute some code irrespective of whether the primary program logic itself succeeds or fails to do its job. In Python we can achieve this using a finally block. A finally block of statement is an optional part of the try-except statements. A code written inside the finally block will ALWAYS be executed.

finally block is majorly used to close the database connections in the programs which involves database connectivity.

Try the below code in python playground and observe the output.

	balance=1000
	amount="300Rs"
	
	def take_card():
	    print("Take the card out of ATM")
	try:
	    if balance>=int(amount):
	        print("Withdraw")
	    else:
	        print("Invalid amount")
	except TypeError:
	    print("Type Error Occurred")
	except ValueError:
	    print("Value Error Occurred")
	except:
	    print("Some error Occurred")
	finally:
	    take_card()

In this topic you learn about the exception, built-in exceptions and handling of exceptions in different cases.

Python: Built-in Exceptions Hierarchy


In Python version 3.9.2 is having the below class hierarchy for built-in exceptions. You will get to know more about each exception and solutions on click of it.

In Python, to learn about the exception handling follow the topic Exception Handling in Python.

Python: Variable Scope


This code has been written in Python to represent the baggage weight check process based on the weight limit specified by an airline.

You can go through the below code and guess the output.

	wt_limit=30
	
	def baggage_check(baggage_wt):
	    extra_baggage_charge=0
	    if not(baggage_wt>=0 and baggage_wt<=wt_limit):
	        extra_baggage=baggage_wt-wt_limit
	        extra_baggage_charge=extra_baggage*100
	    return extra_baggage_charge
	
	def update_baggage_limit(new_wt_limit):
	    wt_limit=new_wt_limit
	    print("This airline now allows baggage limit till",wt_limit,"kgs")
	
	print("This airline allows baggage limit till",wt_limit,"kgs")
	print("Pay the extra baggage charge of",baggage_check(35),"rupees")
	update_baggage_limit(45)
	print("Pay the extra baggage charge of",baggage_check(35),"rupees")	 

Let us go through the code now in more detail to see the scope of variables.

Python scope of variables

extra_baggage and extra_baggage_charge are created inside the function baggage_check(). Hence they are local to that function or in other words, they are local variables. They are created when owning function starts execution and remains in memory till owning function finishes execution. They can be accessed only inside that function.

wt_limit is created outside the functions. Hence it is a global variable. Global variables are created when the program execution starts and remains in memory till the program terminates. They can be read anywhere in the program – within a function or outside. But they are protected from modification inside a function. As it is available throughout the program, use of global variable should be restricted to avoid accidental misuse by developers and to minimize memory usage.

In cases where a global variable needs to be modified inside a function, like in function update_baggage_limit(), Python allows you to do that using the global keyword.

Python Variable Scope

Python: Typs of Argument


Programming languages allow controlling the ordering and default values of arguments. In python we will observe the following in different cases:

Case 1: Positional

This is default way of specifying arguments. In this, the order, count and type of actual arguments should exactly match that of the formal arguments. Else it will result error.

Python: Positional Argument

Case 2: Keyword

This allow flexibility in the order of passing the actual arguments by mentioning the argument name.

Python : Keyword in argument

Case 3: Default

This allows the specify default value for an argument in the function signature. It is used only when no value is passed for the arguments else it works normally. In Python, default arguments should be last in the order.

Python: Default Argument

Case 4: Variable argument count

This allows a function to have variable number of arguments. In Python, any argument name starting with ‘*’ is considered to be a vary length argument. It should be last in order, It works by copying all values beyond that position in a tuple.

Python: Variable argument count

Python: Argument Passing


In programming, there are two ways in which arguments can be passed to functions: 

  • Pass by value
  • Pass by reference

Some languages use pass by value by default while others use pass by reference. Some languages support both and allow you to choose.

In Python, we don’t have to think about pass by value and pass by reference as it does that automatically for you. To emulate this using Python, we use the concept of mutability. If the argument passed is immutable then it follows pass by value, else if the argument passed is mutable then it follows pass by reference.

Note: Till now we have seen int, float, string data types which immutable and mutable data types we will discuss in later part of the topics.

Pass by reference method

Pass by reference case apply on mutable type values, In this case value pass as reference to a method in case any change done with in function will reflect out side the function also. From below example you will see formal and actual argument point to same data location because of reference value.

Python Pass by reference

Pass by value method

Pass by value case apply on immutable variables/arguments in case value change with in the function then changes done on function will not reflect out side of the method. From the below example you will see the storage of formal and actual argument are separate.

Python pass by value method

Functions in Python


Functions are set of instructions to perform a specific task. A function is a block of code which only runs when it is called. You can pass data, known as parameters/argument, into a function. It also called as method.

Syntax

Below is the syntax of functions in python. In the below example you will get more clarity about the function syntax.

Define Function

def function_name([arg1,…,argn]): 
    #statements 
    [return value] 

Call Function
variable_name = function_name([val1,…,valn])

Note: Anything enclosed in [ ] (square bracket) is optional

Function can we call from any program, class or from another functions also.

Example

This is simple example of function, where calculate_sum function is performing the task of sum of two numbers/ passing arguments data1 and data2 and returning final result in value result_sum.

Python Functions Example

Output

30

Returning from a function

Let’s see how we can use values returned from a function.

Python return from a function

In the above screen display the different cases of return value from a function. It’s depend on your task in case your task required to return value then return one result value from function then assign this value to some variable where you call this function.

In case, you need return multiple value you can use reference type variable like array or you can also change value in passing argument if these are reference type and can be access out side the function. You will get more detail of reference variable in next topic Argument passing in Python.

Python: Control Structure


In Python, The commonly used control structures are:

Selection Statements

During the execution of the program, we may not wish to execute all sets of statements sequentially. Sometimes we may wish to select between the set of statements based on some conditions. Based on the test condition evaluation, the flow is determined inside the program. Here is the list of selection statements in Python:

  • If statement: It is a conditional statement used for decision making in python. Example
  • else if statement: It is a conditional statement if block condition is false then execute the else block. Example
  • else if ladder statement: These are multiple else if statement in sequence if one condition not match then go next else for condition check .Example
  • Nested if statement: You can write if block with in another if block that is called nested if. Example

The example of if, else, elif and nested if will discuss in further blogs in more detail.

Looping Statements

Looping statements are used to execute the same block of code multiple times in python based on the test condition. Here is the list of looping statements in python.

  • while loop: The while loop is used to execute a piece of code as long as the test condition is true. While loop is preferred whenever the number of iterations is not known. Example
  • for loop: In python, for loop allows the loop to run over a specific sequence of values. In other words, for every value in the sequence, the loop runs once. Thus we can avoid infinite loops by using a for loop. Example
  • Nested loops: Loop with in another loop is called as nested loop. Example

The example of while loop, for loop and nested loop will discuss in further blogs in more detail.

Loop Control Statements

The flow inside looping statements are controlled using the looping control statements like pass, break and continue.

  • break: When we want to stop a loop or break away from it we can use the break statement. Example
  • continue: When we want to skip the remaining portion of loop statements and continue with the next iteration, we can use the continue statement. Example
  • pass : pass is a null statement that is used to do create empty blocks. When the pass is executed, it results in no operation and the control will move to the next statement applicable. Example

The example of the break, continue, and pass will discuss in further blogs in more detail.

Python: pass Loop Control Statement


The flow inside looping statements are controlled using the looping control statements like pass, break and continue.

In python, pass is a null statement which is used to do create empty blocks. When pass is executed, it results in no operation and the control will move to the next statement applicable.

Example

Below example program shows how pass can be used to create a empty if block.

num=10
count=0
while(count <= num):
   if(count%2 == 0):
	  pass
   else:
	  print(count)
   count+=1

Output

1
3
5
7
9

In the above example try to explain the case of pass statement and treat as empty block. Otherwise same case be handle through continue with same result.

Python: continue Loop Control Statement


The flow inside looping statements are controlled using the looping control statements like pass, break and continue.

When we want to skip the remaining portion of loop statements and continue with the next iteration, we can use continue statement.

Example

Go through the below code, Assume A – Adult passenger, C- Child, FC – Flight Captain, FA – Flight Attendant, SP – Suspicious passenger.

for pasngr in "A","A", "FC", "C", "FA", "SP", "A", "A":
	if(pasngr=="FC" or pasngr=="FA"):
	   print("No check required")
	     continue
	
	if(pasngr=="SP"):
	    print("Declare emergency in the airport")
	    break	
	if(pasngr=="A" or pasngr=="C"):
	    print("Proceed with normal security check")
print("Check the person")
print("Check for cabin baggage")


Output

Proceed with normal security check
Check the person
Check for cabin baggage
Proceed with normal security check
Check the person
Check for cabin baggage
No check required
Proceed with normal security check
Check the person
Check for cabin baggage
No check required
Declare emergency in the airport

In this above example, if you will see when value of passenger is FC or FA then it will execute the statement inside the if block because it’s having continue keyword the the next statement after continue will skip and pointer will reach to next value of for loop.

Same as when passenger will value will be SP then this block having break keyword, it will terminate the loop and stop execution inside the loop and pointer will directly jump to next statement after loop.

Python: break Loop Control Statement


The flow inside looping statements are controlled using the looping control statements like pass, break and continue.

When we want to stop a loop or break away from it we can use break statement.

Example

Go through the below code, Assume A – Adult passenger, C- Child, FC – Flight Captain, FA – Flight Attendant, SP – Suspicious passenger.

for pasngr in "A","A", "FC", "C", "FA", "SP", "A", "A":
	if(pasngr=="FC" or pasngr=="FA"):
	   print("No check required")
	     continue
	
	if(pasngr=="SP"):
	    print("Declare emergency in the airport")
	    break	
	if(pasngr=="A" or pasngr=="C"):
	    print("Proceed with normal security check")
print("Check the person")
print("Check for cabin baggage")


Output

Proceed with normal security check
Check the person
Check for cabin baggage
Proceed with normal security check
Check the person
Check for cabin baggage
No check required
Proceed with normal security check
Check the person
Check for cabin baggage
No check required
Declare emergency in the airport

In this above example, if you will see when value of passenger is FC or FA then it will execute the statement inside the if block because it’s having continue keyword the the next statement after continue will skip and pointer will reach to next value of for loop.

Same as when passenger will value will be SP then this block having break keyword, it will terminate the loop and stop execution inside the loop and pointer will directly jump to next statement after loop.

Python: Nested Loop


A loop within a loop is known as nested loop.

Assume that there are 5 passengers and each of them have 2 baggage. The below code will make sure that all baggage of each passenger have undergone the security check.

Example

num_of_pasngrs=5
num_of_bag=2
security_check=True
for pasngr_count in range(1, num_of_pasngrs+1):
	  for bag_count in range(1,num_of_bag+1):
	      if(security_check==True):
	          print("Security check of passenger:", pasngr_count, "-- baggage:", bag_count,"baggage cleared")
	      else:
	          print("Security check of passenger:", pasngr_count, "-- baggage:", bag_count,"baggage not cleared")

Output

Security check of passenger: 1 — baggage: 1 baggage cleared
Security check of passenger: 1 — baggage: 2 baggage cleared
Security check of passenger: 2 — baggage: 1 baggage cleared
Security check of passenger: 2 — baggage: 2 baggage cleared
Security check of passenger: 3 — baggage: 1 baggage cleared
Security check of passenger: 3 — baggage: 2 baggage cleared
Security check of passenger: 4 — baggage: 1 baggage cleared
Security check of passenger: 4 — baggage: 2 baggage cleared
Security check of passenger: 5 — baggage: 1 baggage cleared
Security check of passenger: 5 — baggage: 2 baggage cleared

The same code in the inner loop can also be written using while loop instead of for loop as shown below:

num_of_pasngrs=5
num_of_bag=2
security_check=True
for pasngr_count in range(1, number_of_passengers+1):
	bag_count =1
	while (bag_count<=num_of_bag):
	   if(security_check==True):
	      print("Security check of passenger:", pasngr_count, "-- baggage:", bag_count,"baggage cleared")
	   else:
	      print("Security check of passenger:", pasngr_count, "-- baggage:", bag_count,"baggage not cleared")
	   bag_count+=1	 

Similarly, the outer loop can also be written using while loop.

Python: for loop


In python, for loop allows the loop to run over a specific sequence of values. In other words, for every value in the sequence, the loop runs once. Thus we can avoid infinite loops by using a for loop.

Syntax

Python for loop

Example

Similar to example of while loop lets handle same scenario with for loop:

	for number in 1,2,3,4,5:
	    print("The current number is ",number) 

Output

The current number is  1
The current number is  2
The current number is  3
The current number is  4
The current number is  5

In the above for loop , the statement inside the loop will execute for each value of number.

Another variation of for loop

In Python, there is an easy way to achieve this by using range(x,y,step). It creates a sequence from x to y-1 with a difference of step between each value. 

Example

	start=1
	end=10
	step=2	
	for number in range (start, end, step):
	    print("The current number is ", number) 

Output

The current number is  1
The current number is  3
The current number is  5
The current number is  7
The current number is  9

In the above example of for loop, the start value will vary from 1 to end-1 i.e 9 and each step increase start value as step 2.

Python: while loop


The while loop is used to execute a piece of code as long as the test condition is true. While loop is preferred whenever the number of iterations is not known.

Syntax

Python while loop

Example

	num=5
	count=1
	while count <= num:
	    print("The current number is:",count)
	    count+=1

Output

The current number is: 1
The current number is: 2
The current number is: 3
The current number is: 4
The current number is: 5

In the above example, while loop will check the condition continuously the value count and compare with num value as long as condition is true. In case of true all the statement inside the while loop will execute.

Python: Nested if Statement


An if statement within another if statement is known as nested if statement. Similarly, any decision logic can be written within an else statement also.

Have a look at the below example of nested if:

Example

num1=10
num2=20
num3=30
	
if(num1>num2):
	if(num1>num3):
      print("num1 is greater")
	else:
	  print("num3 is greater")
elif(num2>num3):
	print("num2 is greater")
else:
	print("num3 is greater")

Output

num3 is greater

in this above example, one if statement is inside of another if block. It’s good example of nested if statement.

Python:else-if Ladder


It is a conditional statement used for selection between multiple set of statements based on multiple test conditions. The various test conditions are provided inside each if statement. Whenever the test condition is evaluated as True, the statements inside the corresponding if block are executed and the control comes out of the else-if ladder. If none of the test conditions are evaluated as True, the statements inside the else block are executed. As we have multiple set of statements to select based on the test conditions, it is also called as multi way selection statement.

In else-if ladder the conditions are evaluated from the top of the ladder downwards. As soon as a true condition is found, the statement associated with it is executed skipping the rest of the ladder.

Below is the syntax of else-if ladder statement:

Python else-if statement

Example

	a=0
	if(a>0):
	    print("positive integer")
	elif(a<0):
	    print("negative integer")
	else:
	    print("it’s zero") 

Output

It’s zero

In this above example, first evaluate the condition in if but a=0 so condition in if statement will result as false. Then it will go to next else if statement to evaluate the condition but the condition evaluation value will be false then it will go to else statement and execute all statement inside of it.

Python: If-Else Statement


It is a conditional statement used for selection between two set of statements based on the evaluation of test condition. The statements inside the if block are executed only if the evaluated condition is true. Otherwise statements inside the else block are executed. As we have two set of statements to select based on the test condition, it is also called as Two-way selection statement.

Below is the syntax of if-else statement:

Python if-else statement

Example

	a=-10
	if(a>0):
		print("positive integer")
	else:
		print("Not a positive integer")

Output

Not a positive integer

In the if statement will evaluate condition and result as false then it will jump to else block then it will execute statement of else block.

Python : If Statement


It is a conditional statement used for decision making in python. In if statement, the test condition is evaluated and the statements inside the if block are executed only if the evaluated condition is true. In if statement, we have only one set of statements to select based on the test condition. So, it is also called as One-way selection statement.

Below is the syntax of simple if statement:

Python if Statement

Example:

a=10
if(a>0):
   print("Positive Integer")

Output

Positive Integer

Python: Excel Read and Write


Python uses openpyxl module to read, write and modify Excel files. This module acts as an interface between our python script and the excel file.

What is Excel File, Workbook, Row, and column?

An Excel file called as Workbook is a collection of sheets called as Worksheets. Each sheet is further divided into rows and columns.

The intersection of a row and a column is called a Cell. Cells hold the data of an Excel sheet.

How Python read & write to excel file?

In order to work with an Excel sheet, we first need to load that excel workbook into our program and then fetch the required worksheet from the workbook object. Once the workbook will load successfully you can point to any sheet in the workbook and then read and write to cell based on selected rows and columns.

In the further section, you will get to know about openpyxl module methods and attributes for each type of case to fetch data or update data to an excel file.

Load Workbook

load_workbook method can be used to load a workbook.

import openpyxl as xl
work_book=xl.load_workbook(r'Employee.xlsx')

Workbook Sheet Names

Once the workbook load successfully, Then you can get the list of sheet names or index the sheets to fetch the worksheet.

  • Sheet Names : sheetnames attribute of workbook object is used to display all the sheet names available in the workbook.
print('Sheet names:',work_book.sheetnames)
  • Specific Sheet: By indexing the sheet name can use to fetch data from specific sheet
work_sheet=work_book['Emp']

Accessing the cells

A cell in a worksheet can be accessed in two ways. 

  • The cell method of worksheet object can be used, we need to pass the row and column numbers as arguments to this method. 
    Note: The row and column numbers will begin from 1.
cell1=work_sheet.cell(1,1)
  • The worksheet object can be indexed with the cell reference to retrieve the corresponding cell object. such as ‘A2’ can be used to select the cell from 2nd row 1st column.
cell2=work_sheet['A2']

Note: The above two methods will return cell objects. The value stored in the cell can be retrieved using the value attribute of the cell object.

cell1.value
cell2.value

Maximum Rows and Columns

To check the maximum number of rows and columns with data in a given sheet. we can use max_row and max_column attributes of the worksheet object.

work_sheet.max_row
work_sheet.max_column

Sheet Slicing

In order to extract a portion of sheet, we need to use slicing. ws[‘A1′:’C4’] is used to extract cells that belong to rows 1 to 4 and columns ‘A’ to ‘C’.

cells=ws['A1':'C4']
for row in cells:
    for cell in row:
        print(cell.value,end=' ')
    print()

This will return a tuple of tuples each representing a row. Every row tuple will consist of cells that represent the columns.

Special Indexing

To retrieve individual rows and individual columns of the worksheet, we are going to use the respective indices of the rows and columns.

Example: ws[1] will retrieve all the cells that belong to 1st row. ws[‘A’] will retrieve all the cells that belong to 1st column.

cells=ws[1]
print('Items of row 1 are:',end=' ')
for cell in cells:
    print(cell.value, end=' ')
cells=ws['B']
print('\nItems of column B are:')
for cell in cells:
    print(cell.value)

This will return a tuple of cells.

Accessing Particular Records

Let us see how to retrieve the record of employee with id 3

import openpyxl as xl
wb=xl.load_workbook(r'Employee.xlsx')
ws=wb['Emp']
cells=tuple()
for row_num in range(2,ws.max_row+1):
    if ws.cell(row_num,1).value==3: 
        cells=ws[row_num]
        break
if cells:
    for cell in cells:
        print(cell.value,end=' ')
else:
    print('Employee record not found')

Note: Iterating is starting from the second row, since the excel sheet has headers in its first row.

Accessing Particular Column

Columns can be accessed in two ways.

  1. If we know the position of the columns, then we can access it using the column reference. 2nd column in a worksheet can be accessed as WS[‘B’]
  2. If we don’t know the column, then we need to validate using the headers and retrieve all the cells.

The below example displays names of all the employees

import openpyxl as xl
wb=xl.load_workbook(r'Employee.xlsx')
ws=wb['Emp']
cells=tuple()
for col_num in range(1,ws.max_column+1):
    if ws.cell(1,col_num).value.lower().strip()=='name': 
        for row_num in range(2,ws.max_row+1):
            cells+=ws.cell(row_num,col_num),
        break
if cells:
    for cell in cells:
        print(cell.value)
else:
    print('No Employees present')

Deleting a specific record

We can delete a record using delete_rows method of worksheet object. It accepts two parameters, 

  1. index – The index at which, we need to delete the row
  2. num – number of rows to be deleted

The below code is used to delete the record of employee with id 3.

import openpyxl as xl
filepath=r'Employee.xlsx'
wb=xl.load_workbook(filepath)
ws=wb['Emp']
for row_num in range(1,ws.max_row+1):
    if ws.cell(row_num,1).value==3: 
        ws.delete_rows(row_num,1)
        wb.save(filepath)
        print('Employee deleted')
        break
else:
    print('Employee not found')

Note: Any changes made will be persisted if and only if the save method of workbook object is invoked. Else, the changes will be lost

Deleting a specific column

We can delete a column using delete_cols method of worksheet object. It accepts two parameters, 

  1. index – The index at which, we need to delete the row
  2. num – number of rows to be deleted

The below code is used to delete salary column.

import openpyxl as xl
filepath=r'Employee.xlsx'
wb=xl.load_workbook(filepath)
ws=wb['Emp']
for col_num in range(1,ws.max_column+1):
    if ws.cell(1,col_num).value.lower().strip()=='salary': 
        ws.delete_cols(col_num,1)
        wb.save(filepath)
        print('Column deleted')
        break
else:
    print('Column not found')

Adding a new record

 To add a new record we need to use append method of worksheet object. append method accepts a list of items which represent a row.

The below code is used to add new employees to the existing sheet.

import openpyxl as xl
filepath=r'Employee.xlsx'
wb=xl.load_workbook(filepath)
ws=wb['Emp']
ws.append([4,'Bharti',10000])
wb.save(filepath)

Creating a new workbook

In order to create a workbook we need to create an object of Workbook class. The following example will help you to create a workbook,create a sheet, remove a sheet. Insert data into it and save it.

import openpyxl as xl
wb=xl.Workbook()
print(wb.sheetnames)
wb.create_sheet(title='Emp', index=0)
print(wb.sheetnames)
wb.remove(wb['Sheet'])
print(wb.sheetnames)
ws=wb['Emp']
 
ws['A1']="Name"
ws['B1']='Salary'
ws.append(['Saurabh',23400.0])
ws.append(['Gaurav',21529.12])
wb.save(r'Employee1.xlsx')

append method is used to insert a new row into the worksheet.
save method is used to save the current workbook.
create_sheet is used to create a new sheet.
remove_sheet is used to remove an existing sheet.

Summary

In this blog we have covered following sections to read and write to excel through Python by using openpyxl module:

  • Load data from workbook
  • Loading complete column and row data
  • Sheet slicing
  • Deleting/Displaying a particular row and column data
  • Create a new work book
  • Add and remove a work sheet from workbook
  • Insert data to worksheet

Python: CSV Read and Write


CSV (Comma Separated Values)?

A comma-separated values (CSV) file is a delimited text file that uses a comma to separate values. A CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format.

How to handle CSV file records in Python?

In Python, there is a library/module named csv which is used to handle CSV files. It consists of methods for read and write operations.

In order to open a CSV file we use open() method. This method accepts two mandatory arguments which are the file name and the mode.

They are three different modes:

  1. Read mode – ‘r‘ is used to open a file in read mode.
  2. Write mode – ‘w‘ is used to open a file in write mode.
  3. Append mode – ‘a‘ is used to open a file in append mode.

Note: Whenever you try to open a file which is not existing on the file system, a new file will be created by the open method. The difference between write mode and append mode is ‘In write mode, the existing contents of the files will be deleted whereas in append mode the existing contents will be retained‘.

The csv module consists of methods that are useful to perform read and write operations.

Read Methods

  • reader() – reader  method accepts file object as an argument and returns a reader object. We can read each record as a list where each element in the list represents a column value.
  • dictReader() – DictReader() accepts file object as an argument and returns a DictReader object. Each record of csv file will be fetched as dictionary where keys represent the column names and values are their corresponding values.

Write Methods

  • writer() – writer method accepts file object as an argument and returns a csv writer object. We need to use writerow method in order to insert records. Lists are used to insert the data into file.
  • dictWriter() – DictWriter accepts filename and field names as arguments. Data which needs to be inserted should be in dictionary format and the keys of dictionary should match with the field names.

Sample CSV File Data

In further example of csv, we are going to use the following data. You can use same by place in text file and change extension of file with .csv

id,name,salary,start_date,dept
1,Saurabh,563.3,2018-01-15,IT
2,Gaurav,326.2,2016-09-23,Operations
3,Ramesh,360,2018-11-15,IT
4,Rajesh,729,2019-05-11,HR
5,Ranjith,986.25,2021-03-27,Finance
6,Rajendra,796,2018-05-21,IT
7,Ajay,423.8,2016-07-30,Operations
8,Bharti,934.5,2015-06-17,Finance

Sample CSV File

Here in the above data first-row, comma-separated values represent column name while the rest of the rows are data for each column.

Reading a CSV File

As explained in the above method definition. You can use these lines of statement to read CSV File.

import csv
   with open(r'C:\Users\saurabh.gupta14\Desktop\Python Example\input.csv','r') as csvfile:
    reader=csv.reader(csvfile)
    for record in reader:
        print(record)

Output

[‘id’, ‘name’, ‘salary’, ‘start_date’, ‘dept’]
[‘1’, ‘Saurabh’, ‘563.3’, ‘2018-01-15’, ‘IT’]
[‘2’, ‘Gaurav’, ‘326.2’, ‘2016-09-23’, ‘Operations’]
[‘3’, ‘Ramesh’, ‘360’, ‘2018-11-15’, ‘IT’]
[‘4’, ‘Rajesh’, ‘729’, ‘2019-05-11’, ‘HR’]
[‘5’, ‘Ranjith’, ‘986.25’, ‘2021-03-27’, ‘Finance’]
[‘6’, ‘Rajendra’, ‘796’, ‘2018-05-21’, ‘IT’]
[‘7’, ‘Ajay’, ‘423.8’, ‘2016-07-30’, ‘Operations’]
[‘8’, ‘Bharti’, ‘934.5’, ‘2015-06-17’, ‘Finance’]

If you noticed in the above example all the rows of CSV file read including column header and got printed.

Read CSV File by using DictReader

import csv
with open(r'C:\Users\saurabh.gupta14\Desktop\Python Example\input.csv','r') as csvfile:
    reader=csv.DictReader(csvfile)
    for record in reader:
        print(record) 

Output

OrderedDict([(‘id’, ‘1’), (‘name’, ‘Saurabh’), (‘salary’, ‘563.3’), (‘start_date’, ‘2018-01-15’), (‘dept’, ‘IT’)])
OrderedDict([(‘id’, ‘2’), (‘name’, ‘Gaurav’), (‘salary’, ‘326.2’), (‘start_date’, ‘2016-09-23’), (‘dept’, ‘Operations’)])
OrderedDict([(‘id’, ‘3’), (‘name’, ‘Ramesh’), (‘salary’, ‘360’), (‘start_date’, ‘2018-11-15’), (‘dept’, ‘IT’)])
OrderedDict([(‘id’, ‘4’), (‘name’, ‘Rajesh’), (‘salary’, ‘729’), (‘start_date’, ‘2019-05-11’), (‘dept’, ‘HR’)])
OrderedDict([(‘id’, ‘5’), (‘name’, ‘Ranjith’), (‘salary’, ‘986.25’), (‘start_date’, ‘2021-03-27’), (‘dept’, ‘Finance’)])
OrderedDict([(‘id’, ‘6’), (‘name’, ‘Rajendra’), (‘salary’, ‘796’), (‘start_date’, ‘2018-05-21’), (‘dept’, ‘IT’)])
OrderedDict([(‘id’, ‘7’), (‘name’, ‘Ajay’), (‘salary’, ‘423.8’), (‘start_date’, ‘2016-07-30’), (‘dept’, ‘Operations’)])
OrderedDict([(‘id’, ‘8’), (‘name’, ‘Bharti’), (‘salary’, ‘934.5’), (‘start_date’, ‘2015-06-17’), (‘dept’, ‘Finance’)])

Writing to CSV File

Here is the line of code to write to a CSV File. To write in CSV file use mode as ‘a’ to append to an existing file if you want to rewrite the complete new CSV file use mode as ‘w’.

import csv
with open(r'C:\Users\saurabh.gupta14\Desktop\Python Example\input1.csv','w') as csvfile:
	    writer=csv.writer(csvfile)
	    writer.writerow(['id','name','salary','dept'])
	    writer.writerow([1,'Sunny',22001.00,'Operation'])
	    writer.writerow([2,'Jony',26501.00,'HR'])


Output

Write to csv with new line

If you have observed the above output, we get a blank line between every record. This is because writerow method inserts a newline after every insertion and also open method also inserts a newline character. It can be removed as follows by adding newline=” in open method:

import csv
with open(r'C:\Users\saurabh.gupta14\Desktop\Python Example\input1.csv','w',newline='') as csvfile:
	    writer=csv.writer(csvfile)
	    writer.writerow(['id','name','salary','dept'])
	    writer.writerow([1,'Sunny',22001.00,'Operation'])
	    writer.writerow([2,'Jony',26501.00,'HR'])


Output

CSV file without new line

Write to CSV file using DictWriter

import csv
with open(r'C:\Users\saurabh.gupta14\Desktop\Python Example\input2.csv','w',newline='') as csvfile:
	    fields=['id','name','salary','dept']
	    writer=csv.DictWriter(csvfile,fields)
	    writer.writeheader()
	    writer.writerow({'id':1,'name':'Raghav','salary':22001.00,'dept':'HR'})
	    writer.writerow({'id':2,'name':'Rajendra','salary':26501.00, 'dept':'Opearation'})

Note: The key of dictionary should exactly map with the name in the fields name.

Output

Summary

In this blog you have learn about the different cases of CSV handling through Python csv module.

  • Read operation through csv
  • Write operation through csv

“Learn From Others Experience"

%d bloggers like this: