Python Forum
help me understand where this comes from
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help me understand where this comes from
#1
hello all, Im a noob to python and raspberry pi. Ive started playing around with some of the modules. the sensor is AM2302 im using adafuit dht library
When running simple temp and humidity script I see the output temp is in C. I would like to change F. Is there a way to do that in the following line?
print("Temp={0:.1f}C  Humidity={1:0.1f}%".format(temperature , humidity))
Im not understanding the 0:0.1f or the 1:0.1f .... is the f for float?



heres the full code

import Adafruit_DHT
import time
 
DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4
 
while True:
    humidity, temperature = Adafruit_DHT.read(DHT_SENSOR, DHT_PIN)

    if humidity is not None and temperature is not None:
       print("Temp={0:.1f}C  Humidity={1:0.1f}%".format(temperature , humidity))
       time.sleep(3);
buran write Nov-11-2020, 07:04 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
Fahrenheit (°F) = (Celsius x 1.8) + 32
Reply
#3
(Nov-11-2020, 06:34 PM)bthurston Wrote: Im not understanding the 0:0.1f or the 1:0.1f .... is the f for float?
https://docs.python.org/3.9/library/stri...i-language

As to converting to Fahrenheit
https://github.com/adafruit/Adafruit_Cir...pletest.py
but you have probably seen it already
(Nov-11-2020, 06:34 PM)bthurston Wrote: Is there a way to do that in the following line?
You can use f-string and directly convert C temperature in the print
print(f"Temp={temperature * 9 / 5 + 32:.1f}F  Humidity={humidity:0.1f}")
But it would be better and more readable to convert temperature on a separate line and then print

temperature_f = temperature * 9 / 5 + 32
print(f"Temp={temperature_f:.1f}F  Humidity={humidity:0.1f}")
bowlofred likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
The {0:.1f} is a format specifier.

https://docs.python.org/3/tutorial/inputoutput.html

print("Temp={0:.1f}C Humidity={1:0.1f}%".format(temperature , humidity))
             ^position 0       ^position 1
When this is printed the expressions (things in the curly brackets) are replaced with values in the .format(). The 0: and 1: say which value goes where. temperature goes to the 0: (right after Temp=) and humidity goes to the 1: (after Humidity=).
print("Temp={0:.1f}C Humidity={1:0.1f}%".format(temperature , humidity))
  conversion type^          width^ ^precision
You are right about the "f" being float. This is a conversion type. If temperature was an integer the .1f converts it to a float before printing.

The width specifier sets how much space is provided to print a value. If the length of what gets printed is less than width, extra padding is added. You can specify the padding character. it defaults to a blank space.

The precision specifier sets how many digits appear right of the decimal.
Reply
#5
Thanks so much, that helps me make more sense of whats going on!
Reply
#6
thanks so much. that really helps me understand whats going on
Reply
#7
If variable name happens to be same as needed in output (like 'humidity' in this case) one can save some keystrokes with f-string self-documenting and debugging feature (requires 3.8 <= Python):

>>> humidity = 100
>>> f'{humidity=:.1f}'
'humidity=100.0'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020