I’ve been learning Python for about a week now using Learn Python the Hard Way. It isn’t that hard for me yet since I have a strong SysAdmin background. So it comes somewhat natural for me.
LPTHW is not the best resource for a beginner but it’s a great start nonetheless. Previously, I haven’t written any code beyond Hello World and today I was able to write a program that determines your age.
While it’s nothing complicated, I do find it very encouraging to create something from scratch.
# Function to find current age. def find_age(current_year, birth_year): print "Subtracting %d - %d" % (current_year, birth_year) return current_year - birth_year # Get input from user for the current calendar year. print "What year is it now?" current_year = int(raw_input("> ")) # Get input from user on the year they were born. print "What year were you born?" birth_year = int(raw_input("> ")) # Create a variable of the user's age. age = find_age(current_year, birth_year) # Display the user's age after calculation. print "You are %d years old." % age
This is the result of the code:
What year is it now? > 2013 What year were you born? > 1990 Subtracting 2013 - 1990 You are 23 years old.
The next step for me is to continue the exercises from LPTHW and then move onto another learning resource.