Introduction to Python: A Beginner’s Guide with Interactive Examples
Introduction to Python
Interactive Story
Let’s create a simple interactive story using Python:
name=input( "Enter your name: " )
age = input("Enter your age: ")
print (name + " is " +age+ " years old ")
print("Hello, your name is " + name + " and you are " + age + " years old.")
#We are going to make an interactive story
character = input("Enter a character: ")
place = input("Enter a place: ")
thing = input("Enter a thing: ")
adjective = input("Enter an adjective: ")
print("The " + character + " went to the " + place + " to get a " + thing + ".")
print("The " + thing + " was heavy and " + adjective + ".")
print("The " + character + " decided the " + thing + " was too " + adjective + ".")Working with Strings and Numbers
word="hello"
phrase="I wish we had holiday next week"
story='''Hello, today the winter is finishing.
Spring is coming.'''
#When we want to write more than one line in the same variable we can use '''
print(word)
print(phrase)
print(story)
#problem of 5 lines of asterisks
print('''* * * * *
* * * * *
* * * * *
* * * * *
* * * * *''')
# concatenation of 2 strings
a="30"
b="10"
print(a+b)
#integers
a=30
b=10
print(a+b)
#problem of fruits
apples=10
oranges=7
bananas=5
kiwi=12
print("We have to bought in total" + str(apples+oranges+bananas+kiwi)+"fruits")
#to concatenate strings and integers, we must transform with str
greeting="hello, my age is "
age=42
print(greeting+str(age))
#Last problem if a=15; b=10, try to obtain 15015 and 2115
a=15
b=10
print(str(a)+str(b)+str(a))
print(str(a+b) + str(a))
#Float is number with decimal figures
c=5/2
print(c)
print(int(c))
print(str(c))
a=15
print(float(a))Comparison Operators and Conditional Statements
#== is a comparison operation, and the result will be True or False
print(10+5==15)
#problem three sides of a right triangle:
a=5
b=3
c=4
print(a**2==b**2+c**2)
#the operator!= is opposite, different from
print(10+5!=15)
#problem: given two number a and b, return True if a is not divisible by b. Float is for decimals
a=float(input("tell me the first number to divide(dividend)= "))
b=float(input("Tell me the second number to divide= "))
print (a%b==0)
#Or it can also be
a=input("tell me the first number to divide(dividend)= ")
b=input("Tell me the second number to divide= ")
print(float(a)%float(b)==0)
#the operator % means the module or rest of a division
#given a number n, it must return True if it is an even two digit number; False otherwise
n=int(input("enter a number to check:"))
print(n%2==0 and 10<=n<100)
#Given a number n, it must return True if it is an even two digit number, or odd number with three figures
n=int(input("Enter a number to check:"))
print(n%2==0 and 10<=n<100 or n%2!=0 and 100<=n<1000)Password Guessing and Multiple Conditions
#Guessing the password of program
password="coras19"
passwordcheck=input("Enter the password: ")
if passwordcheck==password:
print("correct password")
else:
print("Not correct password, try again")
#Multiple if, independent statements
x=int(input("enter a number: "))
if x%2==0:
print("it is an even number")
print("it is divisible by 2")
if x%3==0:
print("it is divisible by 3")
else:
print("It is not divisible by 3")
#ages
x=int(input("enter your age: "))
if x<16:
print("you cannot drive")
if x<18:
print("you cannot vote")
if x<25:
print("you cannot rent a car")
else:
print("you can do everything")
#if, elif, else
x=float(input("enter your note: "))
if x>=9:
print("Your grade is A")
elif 7<=x<9:
print("Your grade is B")
elif 6
More Conditional Statements: Siblings and Games
#Siblings
older=int(input("Say the number of older siblings: "))
younger=int(input("Say the number of younger siblings: "))
if older==0 and younger ==0:
print("You don't have siblings")
elif older==0 and younger !=0:
print("You are the older sibling")
elif older!=0 and younger ==0:
print("You are the younger sibling")
else:
print("you are the middle child")
#Weekday and weekend
day=input("Enter one day of the week: ")
if day=="sunday" or day=="saturday":
print("this is a weekend day")
else:
print("this is a weekday")
#Rock paper and scissors
Jack=input("Enter the Jack's choice: ")
Jill=input("Enter the Jill's choice: ")
if Jack=="rock" and Jill=="paper":
print("Jill wins")
elif Jack=="rock" and Jill=="scissors":
print("Jack wins")
elif Jack=="paper" and Jill=="scissors":
print("Jill wins")
elif Jack=="paper" and Jill=="rock":
print("Jack wins")
elif Jack=="scissors" and Jill== "rock":
print("Jill wins")
elif Jack=="scissors" and Jill== "paper":
print("Jack wins")
else:
print("tie")
