Python basic programs to practice : print a welcome message, name of a person and addition of three numbers

Python is an interesting language. Basic level of programming in python deals with arithmetic operations, mathematical expressions and strings.

Some of the sample programs are listed below.

  • ·       Print a welcome message.
  • ·       Get the firstname and lastname from the user and print it together.
  • ·       Add three numbers

1.Python program to print a welcome message:

              This program prints a welcome message in the output screen.

Eg:

print(“Welcome to the world of python”)

Execute this code in the python interpreter, you get the message as output.


2.Python program to get the firstname and lastname and print it together:

#First,get the value for first name. Here, f_name is the variable for firstname. input() is the method to get the value at run time.

f_name = input(“Enter your first name”)

#Next statement reads the last name from the command prompt and store it to l_name.

l_name = input(“Enter your last name”)

#This prints the name with hello message.

print(“Hello”, f_name + l_name)

This program gives you the following output.


3.Python program to add three numbers:

              This program get three numbers as input. Adds the three numbers and store it into ‘sum’ variable. The sum is displayed using print statement.

#Get the three numbers as input. Here, x,y,z are three variables. “input” function is used to get the input at runtime. To convert the input to integers, we use (int).

x = (int)(input("Enter the first number"))

y = (int)(input("Enter the second number"))

z = (int) (input("Enter the third number"))

#sum is a variable which adds three variables and store the sum value.

sum = x + y + z

# This statement prints the value.

print ("The sum of three numbers is:",sum)

Here, is the output.

These are the basic python programs to execute.
  

 



No comments:

Post a Comment