Posted 2 years ago
·
Author
# Print Statements name = input("Enter your name: ") '''Takes input from user and stores into variable: name''' age = input("Enter your age: ") '''Takes input from user and stores into variable: age''' print("Hello " + name + "! You are " + age + ".") '''Prints using input information'''
# Simple Calculations num1 = input("Enter the first number: ") num2 = input("Enter the second number: ") result = int(num1) + float(num2) '''Input from users in python are automatically converted to strings. In order to add the numbers we need to convert the strings to numbers. One thing we can do is result = int(num1) + int(num2). Remember that int is just for whole number. If your input number contains a decimal, we must use float.''' print(result)
# If Statements is_male = True is_tall = False '''Boolean variable set to true''' if is_male: print("You are a male.") '''it wouldn't have printed anything if is male was set to false.'''
if is_male: print("You're a male.") else: print("You're not a male.")
print("\n")
if is_male or is_tall: print("You're a male or tall or both.") else: print("You're neither male nor tall")
print("\n")
if is_male and is_tall: print("You're a male and tall.") elif is_male and not(is_tall): print("You're a short male.") elif not(is_male) and is_tall: print("You're a short but not a male.") else: print("You're either not male or not tall or both.")
# While loop i = 1 while i <= 10: print(i) i = i + 1 '''short hand for i = i + 1 is i += 1 in python'''
print("Done with loop."
# For Loops
for letter in "sammy": print(letter)
'''For each letter in sammy I want to print out a letter. This prints out all the letters in the name.'''
print("\n")
friends = ["Jim", "Karen", "Pam"] for name in friends: print(name)
for index in range(3, 10): print(index) '''prints numbers 3 - 10'''
for index in range(len(friends)): print(friends[index]) '''len(friends) = 3 so it prints out 0, 1, 2 values.'''