Found 10784 Articles for Python

Binary to decimal and vice-versa in Python

AmitDiwan
Updated on 11-Aug-2022 11:22:14

747 Views

In this article, we will see how to convert Binary to Decimal and Decimal to Binary. Binary is the simplest kind of number system that uses only two digits of 0 and 1 (i.e. value of base 2). Since digital electronics have only these two states (either 0 or 1), so binary number is most preferred in modern computer engineer, networking and communication specialists, and other professionals. Decimal number system has base 10 as it uses 10 digits from 0 to 9. In decimal number system, the successive positions to the left of the decimal point represent units, tens, hundreds, ... Read More

Quine in Python

AmitDiwan
Updated on 12-Aug-2022 12:17:14

1K+ Views

The Quine is a program, which takes no input, but it produces output. It will show its own source code. Additionally, Quine has some conditions. We cannot open the source code file inside the program. Example 1 Here a simple string formatting is working. We are defining a variable ‘a’, and inside a, we are storing ‘a=%r;print (a%%a)’ Then we are printing the value of a, and also replacing %r with the value of a. Thus the quine is working − a='a=%r;print (a%%a)';print (a%a) Output a='a=%r;print (a%%a)';print (a%a) Example 2 We defined a variable _ and assigned ‘_=%r;print ... Read More

Handling missing keys in Python dictionaries

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

1K+ Views

In Python there is one container called the Dictionary. In the dictionaries, we can map keys to its value. Using dictionary the values can be accessed in constant time. But when the given keys are not present, it may occur some errors. In this section we will see how to handle these kind of errors. If we are trying to access missing keys, it may return errors like this. Example code Live Demo country_dict = {'India' : 'IN', 'Australia' : 'AU', 'Brazil' : 'BR'} print(country_dict['Australia']) print(country_dict['Canada']) # This will return error Output AU --------------------------------------------------------------------------- KeyErrorTraceback (most ... Read More

JSON Formatting in Python

Samual Sam
Updated on 26-Jun-2020 12:26:45

8K+ Views

The JSON (Java Script Object Notation) is light weight, well accepted data interchange format. Using JSON formatting techniques in Python, we can convert JSON strings to Python objects, and also convert Python Objects to JSON strings.To use these functionalities, we need to use the json module of Python. The json module comes with the Python standard library. So at first we have to import it first.import jsonConverting Python objects to JSON StringIn the json module, there are some methods like dump(), and dumps() to convert Python objects to JSON strings. The dump() method takes two arguments, the first one is ... Read More

Type Conversion in Python

Samual Sam
Updated on 30-Jul-2019 22:30:23

1K+ Views

Using Python, we can easily convert data into different types. There are different functions for Type Conversion. We can convert string type objects to numeric values, perform conversion between different container types etc. In this section we will see how the conversions can be done using Python. Converting String to Numeric Types To convert from String type objects to Numeric Objects, there are different methods like int(), float() etc. Using the int() method we can convert any number as string to integer value (base 10). It takes the string type argument, default base is 10, We can also specify the ... Read More

Operator Functions in Python

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

644 Views

In Python there are some additional standard library methods for mathematical operations, like arithmetic, logical, relational, bitwise etc. operations. These methods can be found under the operator module. To use it at first we need to import it the operator standard library module. import operator In this section we will see some operator functions for bitwise operations and container operations. Arithmetic Operations At first we will see the arithmetic operating functions. These are like below. Sr.No Functions & Description 1 add(x, y) The add() method is used to add two numbers x and y. ... Read More

Merge two sorted arrays in Python using heapq?

Samual Sam
Updated on 26-Jun-2020 12:19:06

420 Views

In this section we will see how two sorted lists can be merged using the heapq module in Python. As an example, if list1 = [10, 20, 30, 40] and list2 = [100, 200, 300, 400, 500], then after merging it will return list3 = [10, 20, 30, 40, 100, 200, 300, 400, 500]To perform this task, we will use the heapq module. This module comes with Python as Standard Library Module. So we need to import it before using it.import heapqThe heapq module has some properties. These are like below −Method heapq.heapify(iterable)It is used to convert an iterable dataset ... Read More

Creating child process using fork() in Python

karthikeya Boyini
Updated on 26-Jun-2020 12:19:41

2K+ Views

Our task is to create a child process and display process id of both parent and child process using fork() function in Python.When we use fork(), it creates a copy of itself, it is a very important aspect of LINUX, UNIX. fork() is mainly applicable for multithreading environment that means the execution of the thread is duplicated created a child thread from a parent thread. When there is an error, the method will return a negative value and for the child process, it returns 0, Otherwise, it returns positive value that means we are in the parent process.The fork() module ... Read More

Python program to check if a string is palindrome or not

karthikeya Boyini
Updated on 26-Jun-2020 09:38:31

3K+ Views

Given a string, our task is to check weather this string is palindrome or not.AlgorithmStep1: Enter string as an input. Step2: Using string slicing we reverse the string and compare it back to the original string. Step3: Then display the result.Example codemy_string=input("Enter string:") if(my_string==my_string[::-1]):    print("The string is a palindrome") else:    print("The string isn't a palindrome")OutputEnter string:madam The string is a palindrome Enter string:python The string isn't a palindrome

Turtle programming in Python

karthikeya Boyini
Updated on 27-Aug-2023 03:38:10

41K+ Views

Turtle is a special feature of Python. Using Turtle, we can easily draw in a drawing board.First, we import the turtle module. Then create a window, we create a turtle object, and using the turtle() method we can draw on the drawing board. Some turtle method METHOD PARAMETER DESCRIPTION Turtle() None It creates and returns a new turtle object forward() amount It moves the turtle forward by the specified amount backward() amount It moves the turtle backward by the specified amount right() angle It turns the turtle clockwise ... Read More

Advertisements