Python break statement is used to terminate or stop the execution of a loop based on some condition. As soon as the loop encounters break keyword, it is terminated and the execution resumes from the first line after the loop. break use case Suppose there is a large list of names, a dictionary of student objects or a tuple of numbers and you are searching for a particular value in these collections. Now if the element you are searching is found at the second position, why should you check all the other elements. In such a scenario, break is used. break is also used toRead More →

Converting a hexadecimal string to decimal or integer format is often required in programming applications. A common requirement is to convert IP addresses in hex to their decimal form. This article will explain the conversion of a string from hex to decimal in python. Hex to decimal conversion Python library provides an int function which can be used to convert a hex value into decimal format. It accepts 2 arguments. First is the number that needs to be converted. It may be supplied as an integer or a string. Second is the base of the first argument. int returns an integer representation(or base 10 equivalent)Read More →

Python library provides an int function which is used To convert a python string to an integer. To convert a number of a given base to a decimal. Here, decimal does not mean floating numbers but a number having base 10. int function arguments int function accepts 2 arguments. First is the number, it may be supplied as an integer or a string. Second is the base(or number system) of the first argument. Syntax Based on the above explanation, syntax of int function is int(number, base) where number may be an integer or a string which can be converted to an integer. Both arguments are optional.Read More →

Many times a program tries to read from a file but the file has been removed or deleted from its location. Result is that the program crashes abnormally. Best way to handle such situations is to ensure that file exists before beginning any file related operations. This post will list out methods that can be used in python to check for file existence. Method 1: Using os.path.isfile function os.path module has an isfile function that takes the path of the file in string format. It returns True if the file exists at the specified path and False if the file is not present or theRead More →

Many times we need to create a searching utility program which when provided a directory path lists all the files and sub-directories inside it. Python provides many utility functions to list folders and files in a directory. Method 1: Using os.listdir os module has a function listdir which takes the path of directory as argument and returns a list of all files and directories inside it. This function returns a list of names of files and folders. If you want a complete path of both files and folders, then use path function of os.path module to create it. Example, import os file_list = os.listdir(‘f:/python’) print(‘—Read More →

Reading a text file is a vital part in programming. Mostly when a file is read, its contents are stored in a data structure such as an array, list, map etc. This post will detail out various methods to read a text file line by line and store its contents directly into a list in python. For a deep understanding about reading and writing files in python, refer this tutorial Text file that we will be using as an example in this post is named “codippa.txt” and contains following sample content Hello thereThis is pythonLearning from codippa.com Method 1: Using readlines()Open a file in readRead More →

What is a prime number? A number greater than 1 which is divisible only by 1 and itself is called a Prime number. By being divisible means when a number is divided by another number the remainder is 0. Thus, the only factors of a prime number are 1 and the number itself. Remember that 1 is not considered a prime number. There are different methods to check for a prime number in python. This post shall cover all of those. Method 1: Using for-else block A for-else block consists of a for loop block followed by the else block where else block is onlyRead More →

Often it is required to convert a string to an integer but before conversion it is recommended to check if the string being converted is in integer format or not. This might be useful when converting a user input value into an integer since user entered value will always be a string. If the string is not integer then the conversion process will fail resulting in code break. This is because it is possible to convert “12” into 12 but trying to convert ‘twelve’ into a integer will result in an error. This post shall list down different methods by which we can check aRead More →

What is a nested method If a method is defined inside another method, the inner method is said to be nested inside the outer method or it is called Nested method. All languages do not support nesting a method inside another method but python allows you to do so. Benefit Suppose there are some lines of code inside a function(or method) which are repeating multiple times. Instead of repeating them, you can put them inside a separate method and call that method where those lines are required. This prevents repeating lines of code as they are encapsulated in a single method which is called whereRead More →