Pythonista has Published 40 Articles

How to convert an integer to an ASCII value in Python?

Pythonista

Pythonista

Updated on 25-Feb-2020 11:16:05

1K+ Views

ASCII character associated to an integer is obtained by chr() function. The argument for this function can be any number between 0 to 0xffff>>> chr(0xaa) 'ª' >>> chr(0xff) 'ÿ' >>> chr(200) 'È' >>> chr(122) 'z'

How to convert a single character to its integer value in Python?

Pythonista

Pythonista

Updated on 25-Feb-2020 11:14:47

509 Views

Each character is associated with an ASCII value which is a unique number. It is obtained by ord() function.>>> ord('A') 65 >>> ord('+') 43 >>> ord(' ')

How we can create a dictionary from a given tuple in Python?

Pythonista

Pythonista

Updated on 25-Feb-2020 11:14:12

315 Views

We can use zip() function to produce an iterable from two tuple objects, each corresponding to key and value items and then use dict() function to form dictionary object>>> T1=('a', 'b', 'c', 'd') >>> T2=(1, 2, 3, 4) >>> dict((x, y) for x, y in zip(t1, t2))Dictionary comprehension syntax can ... Read More

How do we convert a string to a set in Python?

Pythonista

Pythonista

Updated on 25-Feb-2020 11:12:57

532 Views

Python’s standard library contains built-in function set() which converts an iterable to set. A set object doesn’t contain repeated items. So, if a string contains any character more than once, that character appears only once in the set object. Again, the characters may not appear in the same sequence as ... Read More

why Python can't define tuples in a function?

Pythonista

Pythonista

Updated on 18-Feb-2020 10:38:44

157 Views

Since Python 3.0, it is no longer possible to define unpacked tuple as a parameter in a function (PEP 3113). It means if you try to define a function as below −def fn(a, (b, c)):    passPython interpreter displays syntax error at first bracket of tuple. Instead, define tuple objects ... Read More

How can we use complex numbers in Python?

Pythonista

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 ... Read More

How do we create python string from list?

Pythonista

Pythonista

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

102 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 to insert a Python tuple in a PostgreSql database?

Pythonista

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 ... Read More

How to insert a Python tuple in a MySQL database?

Pythonista

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 ... Read More

How to convert a Python for loop to while loop?

Pythonista

Pythonista

Updated on 11-Feb-2020 06:47:06

8K+ Views

Unlike while loop, for loop in Python doesn't need a counting variable to keep count of number of iterations. Hence, to convert a for loop into equivalent while loop, this fact must be taken into consideration.Following is a simple for loop that traverses over a rangefor x in range(5):   ... Read More

Advertisements