This 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:
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.
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()
Another Awesome tutorial by you