You are on page 1of 3

Python Exercises, Practice, Solution

Python Variables
Example 1: Assigning values to Variables in Python
Get Area using Python Variables

length = 1
width = 2.20
area = length * width
print ("The area is: " , area)
out put : The area is: 2.2

#List of some different variable types:


x = 123 # integer
x = 123L # long integer
x = 3.14 # double float
x = "hello" # string
x = [0,1,2] # list
x = (0,1,2) # tuple
x = open(‘hello.py’, ‘r’) # file

Example 2
x = 5
y = "SNU"
print(x)
print(y)
Example 3
x = "Python "
y = "is "
z = "awesome"
print(x + y + z)
Example 3 #Many Values to Multiple Variables
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
Input function
Example 4
name= input("Enter your name:")
print("Hello, " + name)

# Taking number 1 from user as int


num1 = int(input("Please Enter First Number: "))

# Taking number 2 from user as int


num2 = int(input("Please Enter Second Number: "))

# adding num1 and num2 and storing them in


# variable addition
addition = num1 + num2

# printing
print("The sum of the two given numbers is {}
".format(addition))
Example 5

# Taking name of the user as input


# and storing it name variable
name = input("Please Enter Your Name: ")

# taking age of the user as input and


# storing in into variable age
age = input("Please Enter Your Age: ")

# printing it
print("The name of the user is {0} and his/her age is
{1}".format(name, age))
Example 6# This program says hello and asks for my name.
print('Hello, world!')
print('What is your name?') # ask for their name
myName = input() #The input() Function
print('It is good to meet you, ' + myName)
print('The length of your name is:')
print(len(myName))
print('What is your age?') # ask for their age
myAge = input()
print('You will be ' + str(int(myAge) + 1) + ' in a year.')

You might also like