Categories
Python Answers

How to suppress scientific notation when printing float values with Python?

Sometimes, we want to suppress scientific notation when printing float values with Python.

In this article, we’ll look at how to suppress scientific notation when printing float values with Python.

How to suppress scientific notation when printing float values with Python?

To suppress scientific notation when printing float values with Python, we can use the string’s format method with the {:f} format code.

For instance, we write:

a = -7.1855143557448603e-17
s = '{:f}'.format(a)
print(s)

We call format with a as the argument.

{:f} will format the number so that all the digits are in the string.

Therefore, s is -0.000000.

Conclusion

To suppress scientific notation when printing float values with Python, we can use the string’s format method with the {:f} format code.

Categories
Python Answers

How to convert [key1,val1,key2,val2] to a dict with Python?

Sometimes, we want to convert [key1,val1,key2,val2] to a dict with Python.

In this article, we’ll look at how to convert [key1,val1,key2,val2] to a dict with Python.

How to convert [key1,val1,key2,val2] to a dict with Python?

To convert [key1,val1,key2,val2] to a dict with Python, we can use the zip and dict functions.

For instance, we write:

a = ['foo', 1, 'bar', 2]
b = dict(zip(a[::2], a[1::2]))

print(b)

We call zip with the slices a that returns the keys and values.

We get all the keys with a[::2].

And we get all the values with a[1::2].

Then we call zip to combine them into a list of key-value tuples.

And then we call dict to convert the list of tuples into a dictionary.

Therefore, b is {'foo': 1, 'bar': 2}.

Conclusion

To convert [key1,val1,key2,val2] to a dict with Python, we can use the zip and dict functions.

Categories
Python Answers

How to fill out a Python string with spaces?

Sometimes, we want to fill out a Python string with spaces.

In this article, we’ll look at how to fill out a Python string with spaces.

How to fill out a Python string with spaces?

To fill out a Python string with spaces, we can use the ljust method.

For instance, we write:

s = 'hi'.ljust(10)
print(s)

to call ljust on 'hi' to fill the string with spaces until its length is 10.

Therefore, s is 'hi '.

Conclusion

To fill out a Python string with spaces, we can use the ljust method.

Categories
Python Answers

How to write to an Excel spreadsheet with Python?

Sometimes, we want to write to an Excel spreadsheet with Python.

In this article, we’ll look at how to write to an Excel spreadsheet with Python.

How to write to an Excel spreadsheet with Python?

To write to an Excel spreadsheet with Python, we can use the Pandas DataFrame’s to_excel method.

We’ve to install the openpyxl in addition to Pandas.

To install it, we run:

pip install openpyxl

For instance, we write:

from pandas import DataFrame

l1 = [1, 2, 3, 4]
l2 = [1, 2, 3, 4]
df = DataFrame({'Stimulus Time': l1, 'Reaction Time': l2})
df.to_excel('test.xlsx', sheet_name='sheet1', index=False)

We have 2 lists l1 and l2.

Then we create a DataFrame with the DataFrame constructor.

We pass in a dictionary with l1 and l2 as values as the argument.

Next, we call to_excel with the file path of the file to write to, we set the sheet_name‘s title.

Conclusion

To write to an Excel spreadsheet with Python, we can use the Pandas DataFrame’s to_excel method.

We’ve to install the openpyxl in addition to Pandas.

Categories
Python Answers

How to check file size in Python?

Sometimes, we want to check file size in Python.

In this article, we’ll look at how to check file size in Python.

How to check file size in Python?

To check file size in Python, we can use the os.path.getsize method.

For instance, we write:

import os

b = os.path.getsize("file.txt")
print(b)

We call os.path.getsize with the file path of the file to get the size of.

Therefore b is a number in bytes.

Conclusion

To check file size in Python, we can use the os.path.getsize method.