2 - Writing Python Programs Using Raspberry Pi
2 - Writing Python Programs Using Raspberry Pi
python
print 'hello'
python
python3
debug
python3
datetime datetime
print(datetime.now())
ImportError pyjokes
python3
weather weather
lookup 4118
condition
lookup
class CurrentWeather:
weather_data={'Toronto':['13','partly sunny','8 km/h NW'],
'Montreal':['16','mostly sunny','22 km/h W'],
'Vancouver':['18','thunder showers','10 km/h NE'],
'New York':['17','mostly cloudy','5 km/h SE'],
'Los Angeles':['28','sunny','4 km/h SW'],
'London':['12','mostly cloudy','8 km/h NW'],
'Mumbai':['33','humid and foggy','2 km/h S']
}
def getTemperature(self):
return self.weather_data[self.city][0]
def getWeatherConditions(self):
return self.weather_data[self.city][1]
def getWindSpeed(self):
return self.weather_data[self.city][2]
CurrentWeather
CurrentWeather London
londonWeather = CurrentWeather('London')
londonWeather
CurrentWeather 'London' init
London
self.city
weatherLondon.getTemperature()
'12'
London
weatherLondon.getWeatherConditions()
'mostly cloudy'
weatherLondon.getWindSpeed()
8 km/h NW
CurrentWeather
weather_data
londonWeather
londonWeather CurrentWeather
'London'
CurrentWeather
def getCity(self):
return self.city
CurrentWeather.py
if __name__ == "__main__":
currentWeather = CurrentWeather('Toronto')
wind_dir_str_len = 2
if __name__ == "__main__":
if
CurrentWeather.py
if
wind_dir_str_len
S
wind_dir_str_len = 2
if currentWeather.getWindSpeed()[-2:-1] == ' ':
wind_dir_str_len = 1
[-2:-1]
1
getCity
currentWeather torontoWeather
currentWeather = CurrentWeather('Toronto')
currentWeather = CurrentWeather('Mumbai')
python3
Chapter 3
getCity
CurrentWeather