Python How To Video Pt 6

Python How ToThis article and video was created because of the requests from Gent2910 and others. You guys have been having problems understanding a few concepts with Python. Specifically you’ve asked for:

  • More examples on unconventional uses for functions
  • Examples on using lists
  • Explanation on how to use elif
  • More on Conditional Expressions
  • How to catch errors, or Python Error Handling

All of these topics and more are covered in the video below.

I will be getting to how to automate a website with Python, but I’m helping everyone along slowly, so we all can benefit.

If you have any specific tutorials you want, leave them in the comment section below. The video is next, and then all of the code used can be found after it.

[adsense]

All of the Code Used in the Video

#!/usr/bin/python3

# If this program is being run on its own then execute the function named main

def multiplyTwo(var1 = 1, var2 = 1, *args):
print(str(var1) + ” times 2 is {}”.format(var1 * 2))
print(str(var1) + ” times 2 is {}”.format(var1 * 2))

listEx = [var1 * 2, var2 * 2]

print(listEx)

for i in args:
listEx.append(i * 2)

return listEx

def howOld(age):
if age > 35:
return “Your Older than Me”
elif age < 35:
return “Your Younger than Me”
else: return “Your as Old as Me”

def animal(yourAnimal): # Testing Conditional Expression

animalPref = ‘Same as me’ if (yourAnimal == ‘Cat’) else ‘Boo Hiss’
return animalPref

def weather(city):
if city == ‘Los Angeles’:
print(‘Sunny in LA’)
elif city == ‘Anchorage’:
print(‘Cold’)
elif city == ‘Pittsburgh’:
print(‘Party Cloudy’)
else:
print(‘Look out the window’)

def divideNStuff(x,y):

try:
divResult = x/y
except ZeroDivisionError as e:
print(“Division by zero!” + e)

except TypeError as e:
print(“I need arguments” + e)

except Exception:
print(“Something went wrong”)

else:
print(str(x) + ” divided by ” + str(y) + ” equals ” + str(divResult))
finally:
print(“Made it”)

def main():
doubles = multiplyTwo(1,2,3,4,5)

for i in doubles:
print(i)

print(howOld(43))

print(animal(‘Cat’))

print(weather(‘Los Angeles’))

divideNStuff(2,5)

if __name__ == “__main__”: main()

One Response to “Python How To Video Pt 6”

  1. Mike Joes says:

    Another Awesome tutorial by you 🙂

Leave a Reply

Your email address will not be published.

Google+