Found 10784 Articles for Python

How would you make a comma-separated string from a list in Python?

Vikram Chiluka
Updated on 24-Aug-2023 15:03:30

50K+ Views

In Python, a list is an ordered sequence that can hold several object types such as integer, character, or float. In other programming languages, a list is equivalent to an array. Square brackets [] are used to denote it, and a comma (, ) is used to divide two items in the list. In this article, we will show you how to get a comma-separated string from a list using python. − Using join() method Using join() & map() Functions Using join() & List comprehension Assume we have taken a list containing some random elements. We will return ... Read More

How do we create python string from list?

Pythonista
Updated on 18-Feb-2020 09:40:28

103 Views

Python has an in-built join() function that returns a string by joining elements in a sequence object by inserting separator between elements. If we need a string without any separator, we initialize it with null string>>> lst=['h','e','l','l','o'] >>> str='' >>> str.join(lst) 'hello'

How can we use complex numbers in Python?

Pythonista
Updated on 18-Feb-2020 09:41:04

212 Views

A complex number is a pair of real numbers a and b, most often written as a+bi, or a+ib, where i is called the imaginary unit and acts as a label for the second term. Mathematically,  i2 = -1. Sometimes, j is used instead of i.Here’s how a complex number is assigned to a variable:>>> a=5+6j >>> a (5+6j) >>> type(a) Python has a built-in function complex() which returns a complex data type.complex(x) returns a complex number with x as real part and imaginary part as zero. complex(x, y) returns a complex number with x as real part and y ... Read More

How to insert a Python tuple in a MySQL database?

Pythonista
Updated on 18-Feb-2020 08:03:23

2K+ Views

Assuming that MySQL database named as test is present on server and a table named employee is also created. The table has five fields fname, lname, age, gender, and salary.A tuple object containing data of a record is defined ast1=('Mac', 'Mohan', 20, 'M', 2000)To establish interface between MySQL  and Python 3, you need to install PyMySQL module. Then you can set up the connection using following statementsimport PyMySQL # Open database connection db = PyMySQL.connect("localhost", "root", "", "test" ) # prepare a cursor object using cursor() method cursor = db.cursor()Next step is to set up the insert query using data ... Read More

How to insert a Python tuple in a PostgreSql database?

Pythonista
Updated on 18-Feb-2020 08:04:49

1K+ Views

PostgreSql database is by default installed on port number 5432. Python interface to PostgreSql is provided by installing psycopg2 module. Assuming that test database and employee table with fname, sname, age, gender and salary fields is available.First establish connection and obtain cursor object by following statements in Python script.import psycopg2 conn = psycopg2.connect(database = "test", user = "postgres", password = "pass123", host = "localhost", port = "5432") cur = conn.cursor()Data to be inserted in employee table is stored in the form of tuple objectt1=('Mac', 'Mohan', 20, 'M', 2000)Next set up the insert query using this tuplesql="insert into employee values(%s, %s, ... Read More

How we can use Python Tuple within a Tuple?

AmitDiwan
Updated on 11-Aug-2022 08:23:03

8K+ Views

A Tuple can be used within a Tuple easily. Any item of a Tuple can be a Tuple. Tuples are sequences i.e. a collection of objects which ordered and immutable. To access the Tuple within a Tuple, use the square brackets and the index number of that specific inner Tuple. A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The main difference between the tuples and the lists is that the tuples cannot be changed unlike lists. Tuples use parentheses, whereas lists use square brackets. Create a basic Tuple Let us first create a ... Read More

What is correct syntax to create Python dictionary?

AmitDiwan
Updated on 11-Aug-2022 08:20:35

3K+ Views

The correct syntax to create a Python Dictionary is storing values in the form of key:value pairs. On the left of colon, we store keys and on the right, values i.e. key:value Dictionary is enclosed by curly bracket and do not allow duplicates. According to the 3.7 Python update, dictionaries are now ordered. Consider Dictionary as a set of key: value pairs, with the requirement that the keys are unique (within one dictionary). Each key in a Dictionary is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed ... Read More

What is correct syntax to create Python lists?

AmitDiwan
Updated on 11-Aug-2022 08:18:34

3K+ Views

The correct syntax to create a Python List is using the square brackets. Creating a list is as simple as putting different comma-separated values between square brackets. Through this, using Lists you can store multiple items. A list can have integer, string or float elements. With that, we can also create a List with mixed datatypes. The list can be written as a list of comma-separated values (items) between square brackets. Important thing about a list is that the items in a list need not be of the same type Create a Python List with Integer elements We will create ... Read More

How to count elements in a nested Python dictionary?

AmitDiwan
Updated on 11-Aug-2022 08:15:18

16K+ Views

To count elements in a Nested Dictionary, we can use the Built-in function len(). With that, we can also use a function that recursively calls and calculates the elements in an arbitrary depth nested dictionary. Count Elements in a Dictionary Let us first create a Python Dictionary and count its values using the len() method. Here, we have included 4 key-value pairs in the Dictionary and displayed them. Product, Model, Units, and Available are keys of the Dictionary. Except the Units key, all are having String values − Example # Creating a Dictionary myprod = { "Product":"Mobile", "Model": "XUT", ... Read More

How to truncate a Python dictionary at a given length?

AmitDiwan
Updated on 11-Aug-2022 08:12:24

2K+ Views

To truncate a Python Dictionary at a given length, use the itertools module. This module implements a number of iterator building blocks inspired by constructs from APL, Haskell, and SML. For truncating at a given length, we will use the islice() method of the itertools module. The module standardizes a core set of fast, memory efficient tools that are useful by themselves or in combination. Together, they form an iterator algebra making it possible to construct specialized tools succinctly and efficiently in pure Python. Syntax Here’s the syntax − itertools.islice(sequence, stop) or itertools.islice(sequence, start, stop, step) Above, the sequence ... Read More

Advertisements