Found 10805 Articles for Python

How to assign multiple values to a same variable in Python?

Ankith Reddy
Updated on 05-Mar-2020 05:40:07

842 Views

In Python, if you try to do something likea = b = c = [0,3,5] a[0] = 10You'll end up with the same values ina, b, and c: [10, 3, 5]This is because all three variables here point to the same value. If you modify this value, you'll get the change reflected in all names, ie, a,b and c. To create a new object and assign it, you can use the copy module.  examplea = [0,3,5] import copy b = copy.deepcopy(a) a[0] = 5 print(a) print(b)OutputThis will give the output −[5,3,5] [0,3,5]

How do I assign a dictionary value to a variable in Python?

Lakshmi Srinivas
Updated on 05-Mar-2020 05:36:33

13K+ Views

You can assign a dictionary value to a variable in Python using the access operator []. examplemy_dict = {    'foo': 42,    'bar': 12.5 } new_var = my_dict['foo'] print(new_var)OutputThis will give the output −42exampleThis syntax can also be used to reassign the value associated with this key. my_dict = {    'foo': 42,    'bar': 12.5 } my_dict['foo'] = "Hello" print(my_dict['foo'])OutputThis will give the output −Hello

How can we combine multiple print statements per line in Python?

karthikeya Boyini
Updated on 05-Mar-2020 05:08:00

3K+ Views

You can combine multiple print statements per line using, in Python 2 and use the end argument to print function in Python 3.examplePython2.x print "Hello", print " world" Python3.x print ("Hello", end='') print (" world")OutputThis will give the output −Hello worldAnother thing you could do is put all the things in an array and call ''.join(array). examplearr = ["Hello", "world"] print(' '.join(arr))OutputThis will give the output −Hello world

What are Python coding standards/best practices?

Chandu yadav
Updated on 05-Mar-2020 05:03:45

479 Views

You can use the PEP8 guide as a holy grail. Almost all python world uses this guide to write clean understandable and standard python code. This is available as an extension as a linter for all modern text editors. You can check it out at  http://www.python.org/dev/peps/pep-0008/Properly Structure your folders. All projects need proper structuring. This helps organize code better. Python has an opinionated folder structure that you should use.README.rst LICENSE setup.py requirements.txt sample/__init__.py sample/core.py sample/helpers.py docs/conf.py docs/index.rst tests/test_basic.py tests/test_advanced.pyUse doctests. The doctest module searches for pieces of text that look like interactive Python sessions, and then executes those sessions to ... Read More

How can we print multiple blank lines in python?

Samual Sam
Updated on 05-Mar-2020 05:02:36

2K+ Views

We can print multiple blank lines in python by using the character the number of times we need a blank line. For example, If you need 5 blank lines, you can use −Python 2.x: print "" Python 3.x: print("")You can use features like repetition operator in python to make this easier. For example,Python 2.x: print "" * 5 Python 3.x: print("" * 5)All of these commands will print 5 blank lines on the STDOUT.

Using HTML5 Server-Sent Events in a web application

Jennifer Nicholas
Updated on 20-Dec-2019 10:49:19

151 Views

To use Server-Sent Events in a web application, you would need to add an element to the document.The src attribute of the element should point to an URL that should provide a persistent HTTP connection that sends a data stream containing the events.The URL would point to a PHP, PERL or any Python script that would take care of sending event data consistently.ExampleHere is an example showing application that would expect server time.                    /* Define event handling logic here */                                                              

How to Find HCF or GCD using Python?

Vikram Chiluka
Updated on 28-Oct-2022 11:31:48

1K+ Views

In this article, we will show you how to find the HCF (Highest Common Factor) or GCD (Greatest Common Factor) in Python. Below are the various methods to accomplish this task: Using For Loop Using Euclidean Algorithm Using While Loop Using Recursion(Naive method) Handling Negative Numbers in HCF What is H.C.F. or G.C.D? The largest positive integer that perfectly divides the two given numbers is known as the highest common factor (H.C.F.) or greatest common divisor (G.C.D.). Example - The HCF of 12 and 14, is 2. Using For Loop Algorithm (Steps) Following are the Algorithm/steps to be followed to ... Read More

What are Ordered dictionaries in Python?

Jayashree
Updated on 02-Mar-2020 10:21:48

376 Views

An OrderedDict is a dictionary subclass that remembers the order in which its contents are added, supporting the usual dict methods.If a new entry overwrites an existing entry, the original insertion position is left unchanged. Deleting an entry and reinserting it will move it to the end.>>> from collections import OrderedDict >>> d = {'banana': 3, 'apple':4, 'pear': 1, 'mango': 2} >>> od=OrderedDict(d.items()) >>> od OrderedDict([('banana', 3), ('apple', 4), ('pear', 1), ('mango', 2)]) >>> od=OrderedDict(sorted(d.items())) >>> od OrderedDict([('apple', 4), ('banana', 3), ('mango', 2), ('pear', 1)]) >>> t=od.popitem() >>> t ('pear', 1) >>> od=OrderedDict(d.items()) >>> t=od.popitem() >>> t ('mango', 2)

Where are operators mapped to magic methods in Python?

Vikram Chiluka
Updated on 09-Nov-2022 08:05:07

287 Views

In this article, we will explain to you where are operators mapped to magic methods in python. Python Magic methods are special methods that begin and end with double underscores. They are also known as dunder methods. Magic methods are not intended to be invoked directly by you, but rather invocation occurs by the class on a specific action. When you use the + operator to add two numbers, the __add__() method is called internally. Many magic methods in Python are defined by built−in classes. To get the number of magic methods inherited by a class, use the dir() function. ... Read More

What is the difference between operator and method on Python set?

Jayashree
Updated on 02-Mar-2020 10:21:19

327 Views

Python's set object represents built-in set class. Different set operations such as union, intersection, difference and symmetric difference can be performed either by calling corresponding methods or by using operators.Union by method>>> s1={1,2,3,4,5} >>> s2={4,5,6,7,8} >>> s1.union(s2) {1, 2, 3, 4, 5, 6, 7, 8} >>> s2.union(s1)  {1, 2, 3, 4, 5, 6, 7, 8}Union by | operator>>> s1={1,2,3,4,5} >>> s2={4,5,6,7,8} >>> s1|s2  {1, 2, 3, 4, 5, 6, 7, 8}Intersection by method>>> s1={1,2,3,4,5} >>> s2={4,5,6,7,8} >>> s1.intersection(s2) {4, 5} >>> s2.intersection(s1)  {4, 5}Intersection & operator>>> s1={1,2,3,4,5} >>> s2={4,5,6,7,8} >>> s1&s2 {4, 5} >>> s2&s1  {4, 5}Difference method>>> s1={1,2,3,4,5} >>> s2={4,5,6,7,8} >>> s1.difference(s2) {1, 2, 3} >>> s2.difference(s1)  {8, 6, 7}Difference - operator>>> s1={1,2,3,4,5} >>> s2={4,5,6,7,8} >>> s1-s2 {1, 2, 3} >>> s2-s1  {8, 6, 7}

Advertisements