Interchange two numbers

Problem Statement

You are given two integers. Store them into two variables and exchange them. Can it get any easier! Make sure you use tuple to do the task, rather than using any fancy logic. Print the two numbers.

Input Format

Two integers

Output Format

Two integers

Sample Input

2
1

Sample Output

1
2

Concept

A tuple is similar to a list. The difference is that a tuple is immutable i.e. it cannot be changed. Once you assign some value to a tuple, it cannot be changed. Also, no additional members can be added once a tuple is assigned.

Eg:
>> a = 1, # Define a tuple with one member
>> a = (2, 6)
>> a[1] = 1 # Can’t alter the elements in tuple
TypeError: ‘tuple’ object does not support item assignment

For traversing a tuple, the method is similar to that of a list. Tuples are most commonly used to assign more than one variables in one statement i.e. for simultaneous assignment.

Solution

  
# Enter your code here. Read input from STDIN. Print output to STDOUT
a=int(raw_input())
b=int(raw_input())
a,b=b,a
print a
print b

What’s Your Name?

Problem Statement

Let’s learn the basics of Python! You are given the first name and the last name of a person. Your task is to read them and print the following:

Hello firstname lastname! You just delved into python.

It’s that simple!

In Python you can read a line as a string using

s = raw_input()
#here s reads the whole line.  

Input Format The first line contains the first name, and the second line contains the last name.

Constraints
The length of the first and last name ≤ 10.

Output Format Print the output as mentioned above.

Sample Input

Guido
Rossum

Sample Output

Hello Guido Rossum! You just delved into python.

Concept The input read by the program is stored as a string data type. A string is a collection of characters.

Solution

  
# Enter your code here. Read input from STDIN. Print output to STDOUT
f = raw_input()
l = raw_input()
print "Hello" , f ,l + "! You just delved into python."

Arithmetic Operators

Problem Statement

Let’s learn about Python’s arithmetic operators.

First, let’s read two integers:

a = int(raw_input())
b = int(raw_input())

Now, the three basic arithmetic operators are the following:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)

(We’ll learn about division in the next task)

Task
Read two integers from STDIN and print three lines where:
(1) the first line contains the sum of the two numbers,
(2) the second line contains the difference of the two numbers (first – second),
(3) the third line contains the product of the two numbers.

Input Format
The first line contains the first integer, a, and the second line contains the second integer, b.

Constraints
1a1010
1b1010

Output Format
Print three lines as explained above.

Sample Input

3
2

Sample Output

5
1
6

Explanation
3+25
321
326

Solution

  
# Enter your code here. Read input from STDIN. Print output to STDOUT
a = int(raw_input())
b = int(raw_input())
print a+b
print a-b
print a*b

Raw Input

Problem Statement

Let’s look at Python’s input reading method.

>>> name = raw_input("Hey what's your name?\n")
Hey what's your name?
R2D2
>>> print name
R2D2

That’s one way of doing it for yourself. But in competitive programming we don’t ask questions. We only answer them. So we will only use raw_input() to read a string without asking any question to the system.

Task
Read a string from the console and print it.

Input Format
The first and only line of input contains a string.

Constraints
String length ≤ 500

Output Format
Print the same string back.

Sample Input

How many chickens does it take to cross the road?

Sample Output

How many chickens does it take to cross the road?

Solution

  
# Enter your code here. Read input from STDIN. Print output to STDOUT
print raw_input()


Hello World!

Problem Statement

Python is an interactive language that allows us to execute statements individually and see their results. In other words, you don’t have to write a full code and compile it before you execute to see the results as in other languages like C++ and Java.

Here is a sample line of code that can be executed in Python

print "Hello World!" 

You can even store the string in a variable and then print it:

my_string = "Hello World!"
print my_string

The above code will print Hello World! on your screen. You can try it in the console below.

P.S. Do try Solve Me First and Solve Me Second in Python before moving ahead.

Solution

  
#This is a single line comment
'''
This is 
a 
multiline comment
'''
#write your code in next line. 
my_string = "Hello World!"
print my_string