Posts

Showing posts from May, 2024

Basic Errors in python

Image
      A successful developer needs to know how to handle the errors. In python, basic errors are listed below. ·        Indentation error. ·        Syntax error ·        Type error ·        Name error   1.Indentation error: Python follows indentation properly. Let a is assigned to 7 and b is assigned to 4. Just check if a is greater than b, print a. Here, if statement and its block is in same indentation. In python, it is an indentation error. Let us rewrite the code with proper indentation.   2.Syntax error:             This error comes when the user misses the syntax. The below example, user missed colon(:) in the while loop. Let we correct the error putting : after the while loop condition. Here, is the outcome. 3.Type error:           This error occurs, whe...

How to create a user defined package in python???

Image
Package is a collection of files which contains various member functions and its definitions. When a user creates a package with their own definition, it can be considered as a module. It can be reused anywhere. Let us create a package in your system by creating a folder “greetings” by right click in the folder. Choose “new” option and select new folder. Name it as “greetings”. Next, create three files. One is “__init__.py”,other two’s are “greet1.py” and “greet2.py”. It look like below. greetings      |-------     __init__.py         |-------    greet1.py      |-------    greet2.py   __init__.py is a file which represents a initialization file. it denotes the it belongs to a package. greet1.py and greet2.py defines the member functions. Here ,it contains the below code. “greet1.py” #This file has a definition of function “message1” as a print statement. def message1(...

How to find the path of a file or directory is exists or not???

Image
     Directory contains all the files available in the system. Each and every file is specified with a path. This blog gives the way to find the path and its existence. First, list out all the paths available in the system by the below code. #Import the sys module. import sys #print the available system paths print(sys.path) when executing this code, the available paths in system is displayed. ['', 'C:\\Users\\rajeswari jeevananth\\AppData\\Local\\Programs\\Python\\Python311\\python311.zip', 'C:\\Users\\rajeswari jeevananth\\AppData\\Local\\Programs\\Python\\Python311\\DLLs', 'C:\\Users\\rajeswari jeevananth\\AppData\\Local\\Programs\\Python\\Python311\\Lib', 'C:\\Users\\rajeswari jeevananth\\AppData\\Local\\Programs\\Python\\Python311', 'C:\\Users\\rajeswari jeevananth\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages'] Finding the file path available or not by path.exists() method: This method uses a...

How to create, read and write contents to a file???

Image
              In the computer world, a basic thing is called files. It contain set of instructions that can be a text,,image ,commands or any type of information. Generally, files can be of 3 types. ·        System files ·        Application files ·        Data files Based on the usage of information, it can be of any above type. How to create file and write contents to the file in python:               A file can   be created or created file can be opened by the following syntax. File_object = open(“filename”,”mode”) Where File_object is the object from which, we access the file. File name is the name of the file with extension. Mode depends on the operations we perform on file. Mode Purpose “x” It creates a new file. If the fi...

Python enumeration

Image
       Enumeration is one of the concepts in python. It deals with collection of things grouped under a common name. It follows an order. Syntax: enumerate(iterable, StartIndex) where iterable – it is the object.              StartIndex – it is the beginning of list. Default value is 0. Eg: #Creating a fruit list Fruitslist = ['apple', 'banana', 'orange', 'grapes'] #Making the list as a enumerated list F_list = enumerate(Fruitslist) #printing the enumerated list print(list(F_list)) Executing this, the following output is shown in the output screen. When the startindex value is mentioned in the code, it becomes… Fruitslist = ['apple' , 'banana' , 'orange' , 'grapes'] F_list = enumerate(Fruitslist,2) print(list(F_list)) just execute this code,you will get the below output. Python program to print the items in the list using enumeration: Let us create list of vehicles and p...

Python programs using Functions :Factorial of a number and Division of two numbers

Image
  Functions are set of statements for performing a specific task. A function includes a declaration, definition and function call. A function declaration includes a key word ‘def’ follows by a function name and arguments. Syntax: def   function_name(arg1,arg2,…..,argn) where arg1…argn – arguments Note: You can define a function without arguments also. A function call uses the function name and arguments. Eg: function_name(arg1,arg2,…argn) Python program to do division of two numbers using functions: #first, function definition. Here, ‘div’ is the function name. x,y are the arguments. This function performs division of two numbers. def div(x,y):     return x/y #Next, x and y values are read from runtime. x = int(input("Enter the numerator value")) y = int(input("Enter the denominator value")) #The function call div(5,2) control goes to function definition. It return the output to print statement. It prints the output. print(div(5,...