0% found this document useful (0 votes)
3 views9 pages

Python 2

The document provides examples of Python programs for adding two numbers, both with predefined values and user input. It explains the use of the '+' operator and the input() function to gather user input, converting it to float for addition. Additionally, it includes a program for calculating the square root of a number, demonstrating both real and complex number handling using the cmath module.

Uploaded by

iswaryakcs
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Python 2

The document provides examples of Python programs for adding two numbers, both with predefined values and user input. It explains the use of the '+' operator and the input() function to gather user input, converting it to float for addition. Additionally, it includes a program for calculating the square root of a number, demonstrating both real and complex number handling using the cmath module.

Uploaded by

iswaryakcs
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python Program to Add Two

Numbers
To understand this example, you should have the knowledge of the
following Python programming topics:
 Python Basic Input and Output
 Python Data Types
 Python Operators

In the program below, we've used the + operator to add two numbers.

Example 1: Add Two Numbers


# This program adds two numbers

num1 = 1.5
num2 = 6.3

# Add two numbers


sum = num1 + num2

# Display the sum


print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
Run Code

Output

The sum of 1.5 and 6.3 is 7.8

The program below calculates the sum of two numbers entered by the user.
Example 2: Add Two Numbers With User Input
# Store input numbers
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')

# Add two numbers


sum = float(num1) + float(num2)

# Display the sum


print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
Run Code

Output

Enter first number: 1.5


Enter second number: 6.3
The sum of 1.5 and 6.3 is 7.8

In this program, we asked the user to enter two numbers and this program
displays the sum of two numbers entered by user.

We use the built-in function input() to take the input. Since, input() returns
a string, we convert the string into number using the float() function. Then, the
numbers are added.
Alternative to this, we can perform this addition in a single statement without
using any variables as follows.

print('The sum is %.1f' %(float(input('Enter first number: ')) +


float(input('Enter second number: '))))
Run Code

Output

Enter first number: 1.5


Enter second number: 6.3
The sum of 1.5 and 6.3 is 7.8

Although this program uses no variable (memory efficient), it is harder to read.


Python Program to Add Two
Numbers
To understand this example, you should have the knowledge of the
following Python programming topics:
 Python Basic Input and Output
 Python Data Types
 Python Operators

In the program below, we've used the + operator to add two numbers.

Example 1: Add Two Numbers


# This program adds two numbers

num1 = 1.5
num2 = 6.3

# Add two numbers


sum = num1 + num2

# Display the sum


print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
Run Code

Output

The sum of 1.5 and 6.3 is 7.8

The program below calculates the sum of two numbers entered by the user.
Example 2: Add Two Numbers With User Input
# Store input numbers
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')

# Add two numbers


sum = float(num1) + float(num2)

# Display the sum


print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
Run Code

Output

Enter first number: 1.5


Enter second number: 6.3
The sum of 1.5 and 6.3 is 7.8

In this program, we asked the user to enter two numbers and this program
displays the sum of two numbers entered by user.

We use the built-in function input() to take the input. Since, input() returns
a string, we convert the string into number using the float() function. Then, the
numbers are added.
Alternative to this, we can perform this addition in a single statement without
using any variables as follows.

print('The sum is %.1f' %(float(input('Enter first number: ')) +


float(input('Enter second number: '))))
Run Code

Output

Enter first number: 1.5


Enter second number: 6.3
The sum of 1.5 and 6.3 is 7.8

Although this program uses no variable (memory efficient), it is harder to read.


Python Program to Print Hello
world!
To understand this example, you should have the knowledge of the
following Python programming topics:
 Your First Python Program
 Python Basic Input and Output

Source Code
# This program prints Hello, world!

print('Hello, world!')
Run Code

Output

Hello, world!

In this program, we have used the built-in print() function to print the
string Hello, world! on our screen.
By the way, a string is a sequence of characters. In Python, strings are
enclosed inside single quotes, double quotes, or triple quotes.
Python Program to Add Two
Numbers
To understand this example, you should have the knowledge of the
following Python programming topics:
 Python Basic Input and Output
 Python Data Types
 Python Operators

In the program below, we've used the + operator to add two numbers.

Example 1: Add Two Numbers


# This program adds two numbers

num1 = 1.5
num2 = 6.3

# Add two numbers


sum = num1 + num2

# Display the sum


print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
Run Code

Output

The sum of 1.5 and 6.3 is 7.8

The program below calculates the sum of two numbers entered by the user.
Example 2: Add Two Numbers With User Input
# Store input numbers
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')

# Add two numbers


sum = float(num1) + float(num2)

# Display the sum


print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
Run Code

Output

Enter first number: 1.5


Enter second number: 6.3
The sum of 1.5 and 6.3 is 7.8

In this program, we asked the user to enter two numbers and this program
displays the sum of two numbers entered by user.

We use the built-in function input() to take the input. Since, input() returns
a string, we convert the string into number using the float() function. Then, the
numbers are added.
Alternative to this, we can perform this addition in a single statement without
using any variables as follows.

print('The sum is %.1f' %(float(input('Enter first number: ')) +


float(input('Enter second number: '))))
Run Code

Output

Enter first number: 1.5


Enter second number: 6.3
The sum of 1.5 and 6.3 is 7.8

Although this program uses no variable (memory efficient), it is harder to read.


Python Program to Find the
Square Root
To understand this example, you should have the knowledge of the
following Python programming topics:
 Python Basic Input and Output
 Python Data Types
 Python Operators

Example: For positive numbers


# Python Program to calculate the square root

# Note: change this value for a different result


num = 8

# To take the input from the user


#num = float(input('Enter a number: '))

num_sqrt = num ** 0.5


print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))
Run Code

Output

The square root of 8.000 is 2.828


In this program, we store the number in num and find the square root using
the ** exponent operator. This program works for all positive real numbers.
But for negative or complex numbers, it can be done as follows.

Source code: For real or complex numbers


# Find square root of real or complex numbers
# Importing the complex math module
import cmath

num = 1+2j

# To take input from the user


#num = eval(input('Enter a number: '))

num_sqrt = [Link](num)
print('The square root of {0} is {1:0.3f}
+{2:0.3f}j'.format(num ,num_sqrt.real,num_sqrt.imag))
Run Code

Output

The square root of (1+2j) is 1.272+0.786j

In this program, we use the sqrt() function in the cmath (complex math)
module.
Note: If we want to take complex number as input directly, like 3+4j , we have
to use the eval() function instead of float().
The eval() method can be used to convert complex numbers as input to
the complex objects in Python. To learn more, visit Python eval() function.

Also, notice the way in which the output is formatted. To learn more,
visit string formatting in Python.

You might also like