Found 34494 Articles for Programming

How to define a Python dictionary within dictionary?

Pythonic
Updated on 18-Feb-2020 07:38:26

160 Views

A dictionary object is mutable. Hence one dictionary object can be used as a value component of a key. So we can create a nested dictionary object another dictionary object is defined as value associated with key.>>>> students={"student1":{"name":"Raaj", "age":23, "subjects":["Phy", "Che", "maths"],"GPA":8.5},"student2":{"name":"Kiran", "age":21, "subjects":["Phy", "Che", "bio"],"GPA":8.25}}

How you will create your first program in Python?

Pythonic
Updated on 30-Jul-2019 22:30:20

100 Views

You can use any Python aware editor to write Python script. Standard distribution of Python comes with IDLE module which is an integrated development and learning environment. Start IDLE and open a new file from file menu. In the editor page enter print (“Hello World!”) Save the script as hello.py and execute it from Run menu to get Hello world message on console. This is your first program. It can also be run from command prompt c:\user>python hello.py

How to remove a key from a python dictionary?

Pranathi M
Updated on 16-Sep-2022 06:49:27

730 Views

In python, a dictionary is an unordered collection of data which is used to store data values such as map unlike other datatypes that store only single values. Keys of a dictionary must be unique and of immutable data type such as Strings, Integers, and tuples, but the key values can be repeated and be of any type. Dictionaries are mutable, therefore keys can be added or removed even after defining a dictionary in python. They are many ways to remove a key from a dictionary, following are few ways. Using pop(key, d) The pop(key, d) method returns the value ... Read More

How to add new keys to a dictionary in Python?

Pythonic
Updated on 30-Jul-2019 22:30:20

765 Views

Dictionary is an unordered collection of key-value pairs. Each element is not identified by positional index. Moreover, the fact that key can’t be repeated, we simply use a new key and assign a value to it so that a new pair will be added to dictionary. >>> D1 = {1: 'a', 2: 'b', 3: 'c', 'x': 1, 'y': 2, 'z': 3} >>> D1[10] = 'z' >>> D1 {1: 'a', 2: 'b', 3: 'c', 'x': 1, 'y': 2, 'z': 3, 10: 'z'}

How to merge two Python dictionaries in a single expression?

Pythonic
Updated on 30-Jul-2019 22:30:20

110 Views

Built-in dictionary class has update() method which merges elements of argument dictionary object with calling dictionary object. >>> a = {1:'a', 2:'b', 3:'c'} >>> b = {'x':1,'y':2, 'z':3} >>> a.update(b) >>> a {1: 'a', 2: 'b', 3: 'c', 'x': 1, 'y': 2, 'z': 3} From Python 3.5 onwards, another syntax to merge two dictionaries is available >>> a = {1:'a', 2:'b', 3:'c'} >>> b = {'x':1,'y':2, 'z':3} >>> c = {**a, **b} >>> c {1: 'a', 2: 'b', 3: 'c', 'x': 1, 'y': 2, 'z': 3}

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

Pythonic
Updated on 30-Jul-2019 22:30:20

177 Views

Any built-in data type converted into its string representation by str() function >>> str(10) '10' >>> str(11.11) '11.11' >>> str(3+4j) '(3+4j)' >>> str([1,2,3]) '[1, 2, 3]' >>> str((1,2,3)) '(1, 2, 3)' >>> str({1:11, 2:22, 3:33}) '{1: 11, 2: 22, 3: 33}' For a user defined class to be converted to string representation, __str__() function needs to be defined in it. >>> class rectangle: def __init__(self): self.l=10 self.b=10 def __str__(self): return 'length={} breadth={}'.format(self.l, self.b) >>> r1=rect() >>> str(r1) 'length = 10 breadth = 10'

What are different data conversion methods in Python?

Sarika Singh
Updated on 14-Nov-2022 08:22:29

615 Views

Type conversion is the transformation of a Python data type into another data type. Implicit type conversion and explicit type conversion are the two basic categories of type conversion procedures in Python. We will cover the following topics in this article − Implicit Type Conversion in Python is carried out by the Python interpreter automatically. In Python, explicit type conversion must be performed directly by the programmer. Let's study more about these two approaches in depth and with some illustrations. Implicit Type conversions Implicit type conversion occurs when the Python interpreter automatically changes an object's data type without ... Read More

How to print concatenated string in Python?

Pythonic
Updated on 30-Jul-2019 22:30:20

341 Views

When used with strings, plus (+) is defined as concatenation operator. It appends second string to the first string. >>> s1 = 'TutorialsPoint ' >>> s2 = 'Hyderabad' >>> print (s1+s2) TutorialsPoint Hyderabad

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

Pythonic
Updated on 30-Jul-2019 22:30:20

172 Views

When used with strings, asterisk (*) is defined as repetition operator. It concatenates given string as many times as number followed by asterisk. >>> string = 'abcdefghij' >>> print (string*2) abcdefghijabcdefghij

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

Advertisements