Found 33676 Articles for Programming

How you will create your first program in Python?

Niharikaa Aitam
Updated on 29-May-2025 05:01:40

182 Views

When we are starting with Python, mostly created first program will be "Hello, World", which prints the statement "Hello, World" as the output. Before we begin with the first program, we need to install Python in our system from the official website. Choose a Code Editor After installing Python, we need to select the text editor based on our requirements and usage. Following are the different editors available - IDLE: It comes pre-installed with Python VS Code: A popular and lightweight code editor PyCharm: This is useful ... Read More

How to remove a key from a python dictionary?

Niharikaa Aitam
Updated on 28-May-2025 14:52:59

915 Views

A dictionary in Python is a collection of key-value pairs. In a few cases, we may have to remove a key and its associated value from a dictionary. Python provides multiple ways to do this safely and efficiently. Using pop() method The pop() method is used to remove the specified key and returns the corresponding value. If the key does not exist, then it raises a KeyError unless a default value is provided. Example Following is an example, which shows how to remove a key from the dictionary using the pop() method - my_dict = {'a': 1, 'b': 2, 'c': ... Read More

How to add new keys to a dictionary in Python?

SaiKrishna Tavva
Updated on 01-Sep-2025 14:57:36

991 Views

In Python, a Dictionary is one of the data structures that contains key-value pairs enclosed in curly braces '{}'. It is a Mutable and unordered data structure. Before proceeding with adding new keys to a dictionary, first let's create a dictionary with key and values. Following is the example of creating a dictionary with specified key and values - dict1 = {'key1': 'Tutorials', 'key2': 'Point', 'key3': 'Python'} print("Created Dictionary:", dict1) Following is the output of the above example - Created Dictionary: {'key1': 'Tutorials', 'key2': 'Point', 'key3': 'Python'} Adding new keys to a Dictionary Once a Dictionary is ... Read More

How to merge two Python dictionaries in a single expression?

SaiKrishna Tavva
Updated on 01-Sep-2025 14:52:24

248 Views

In Python, a Dictionary is a unordered and muttable data structure which stores data as a key-value pair. In some cases, we may need to merge two dictionaries into a single dictionary by using a single expression, in such scenarios python provides different methods. Using | Merge Operator The | merge operator in Python is used to merge multiple dictionaries into one with a single expression and returns the combined dictionary as a new dictionary. If both dictionaries have the same key, then the value from the dictionary on the right side of the operator will be replaced with the ... Read More

How to change any data type into a string in Python?

Niharikaa Aitam
Updated on 29-May-2025 04:52:26

255 Views

In Python, converting different data types into strings is a common requirement when we want to display or store information in text format. The built-in str() function is used for this conversion. This function accepts any Python object as a parameter and returns a string representation of it. The basic syntax of the str() function is as follows: str(object) Where object is any valid Python object, such as int, float, bool, list, etc. In this article, we will explore different data type conversions into a string in Python. Conversion of Integer to String An integer can be converted into ... Read More

What are different data conversion methods in Python?

Sarika Singh
Updated on 09-Jun-2025 10:17:08

865 Views

Data conversion in Python refers to changing the data type of a value to another data type. Python supports two different types of data conversion, such as Implicit conversion and explicit conversion. Implicit Conversion is performed by the Python interpreter automatically. Explicit Conversion is manually done by the programmer using built-in functions in Python Implicit Conversion Python automatically converts one data type to another, and this type of conversion is called Implicit conversion. To avoid any data loss during runtime, the smaller data type is converted ... Read More

How to print concatenated string in Python?

Niharikaa Aitam
Updated on 29-May-2025 04:20:25

477 Views

When working with strings, we may need to concatenate or join multiple strings together. Python provides several methods to perform the concatenation of strings. Following are various ways to print concatenated strings using a single statement. Using the "+" Operator Using f-string Using format() Method Using join() Method Using print() with Multiple Arguments Using List or Tuple Unpacking Using map() with str Using the "+" Operator The "+" operator is the simplest and most direct way to concatenate two or more strings in Python. It joins the strings end-to-end. Here's an example of using the + operator - ... Read More

How to print a string two times with single statement in Python?

Niharikaa Aitam
Updated on 28-May-2025 15:38:03

274 Views

Printing a String Twice Using Multiplication When we are developing Python, we may want to repeat or duplicate a string for display or formatting purposes. In such cases, Python provides several ways to print a string in defined number of times with a single statement. There are different ways to print a string twice with a single statement in Python. They are - Using Multiplication Operator Using the Concatenation Operator Using the format() Method Using f-string Using join() ... Read More

How to truncate a file in Java?

Swarali Sree
Updated on 20-Feb-2020 09:55:21

1K+ Views

The flush() method of the FileWriter class flushes the contents of the file. You can use this method to truncate a file.Exampleimport java.io.File; import java.io.FileWriter; public class FileTruncate {    public static void main(String args[]) throws Exception {       File file = new File("myData");       FileWriter fw = new FileWriter(file, false);       fw.flush();       System.out.println("File truncated");    } }OutputFile truncated

How to open a plain text file in Java?

Samual Sam
Updated on 20-Feb-2020 09:54:30

342 Views

You can access a plain text using the File class.ExampleLive Demoimport java.io.File; public class ReadFile {    public static void main(String[] args) {       File f = null;       String str = "data.txt";       try {          f = new File(str);          boolean bool = f.canExecute();          String a = f.getAbsolutePath();          System.out.print(a);          System.out.println(" is executable: "+ bool);       } catch (Exception e) {          e.printStackTrace();       }    } }Output C:\Users\data is executable: true

Advertisements