1-Input Your Name Into A Variable Called $name and Then Print "Hello, "

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

1-Input your name into a variable called $name and then print "Hello,

<your name here>".

>>> name= "Riya"

>>> print ("Hello," +name)

Hello,Riya

2-Write a program that adds two numbers and then prints out whether
the sum of those two numbers is positive or negative.

>>> a = int(input("Enter the first number"))

Enter the first number5

>>> b = int(input("Enter the second number"))

Enter the second number4

>>> if( a+b>=0 ):

print("The sum of two no is positive")

The sum of two no is positive


3-Write a program that stores a number and keepstrying to get user input until the user enters
the number correctly. As soon as the correct number is entered, it prints: Correct!

4-Write a program that stores a number and keepstrying to get user


input until the user enters the number correctly. As soon as the correct
number is entered, it prints: Correct!

Type "help", "copyright", "credits" or "license()" for more information.

>>> num=33

>>> while True:

guess=int(input("Guess the number"))

if guess==num:

print("correct")

Guess the number5

Guess the number6


Guess the number4

Guess the number33

Correct

4-Input your first name and last name as two separate variables,
labeled as $firstname and $lastname respectively. Concatenate them
together using the dot operator '.' into a new variable called
$wholename. Then print out the $wholename.

>>>FirstName=”Riya”

>>>LastName=”Virdi”

>>>WholeName=FirstName + ‘.’ +LastName

>>>Print(WholeName)

Riya.Virdi
5-Write a program to accept an input string from the user and toggle
the character cases. For example, $str=” Hello How Are You?” O/p :
hELLO hOW aRE yOU

>>> string=str(input("Enter a string: "))

Enter a string: Hello howare you?

>>> print(string.swapcase())

hELLO HOWARE YOU?

>>>

6- Write a program which will perform sum and multiplication ,that sums and multiplies
(respectively) all the numbers in a list of numbers. For example, sum([1, 2, 3, 4]) should return
10, and multiply([1, 2, 3, 4]) should return 24.
>>> n=int(input("Enter no. of entities: "))

Enter no. of entities: 4

>>> numbers=[int(input())for i in range(n)]


1

>>> sum=0

>>> multi=1

>>> for i in numbers:

sum +=i

multi *=i

>>> print("Sum: ",sum)

Sum: 16

>>> print("Multiply : ",multi)

Multiply : 120
7-Write a programthat takes a value (i.e. a number, string, etc) x and a
list of values a, and returns Trueif x is a member of a, False otherwise.
(Note that this is exactly what the in operator does, but for the sake of
the exercise you should pretend Python did not have this operator.)

>>> x=int(input("Enter a value:"))

Enter a value:4

>>> list=[]

>>> n=int(input("Input the number of elements"))

Input the number of elements6

>>> for i in range(0,n):

ele=int(input())

list.append(ele)

if x in list:

print("True")

else:

print("False")

False

False

False

True

True
6

True

>>>

8-Write a programthat hastwo lists and printTrue if they have at least one member in common,
False otherwise. You may use your is_member() function, or the in operator, but for the sake of
the exercise, you should (also) write itusing two nested for-loops.
>>> n=int(input("Enter the no. of entities in list 1: "))
Enter the no. of entities in list 1: 4

>>> list1=[int(input())for i in range(n)]

>>> m=int(input("Enter the no. of entities in list 2: "))

Enter the no. of entities in list 2: 4

>>> list2=[int(input())for i in range(m)]

>>> result=False

>>> for x in list1:

for y in list2:

if x==y:

result=True

>>> print(result)

True
9-Write a program for histogramthat takes a list of integers and prints
a histogram to the screen. For example, histogram([4, 9, 7]) should
print the following:
****
*********
*******

>>>

n = int(input("Enter no. of entities: "))

Enter no. of entities: 3

>>> print("enter elements: ")

enter elements:

>>> arr=[int(input())for i in range(n)]

7
>>> for i in arr:

print("*"*i)

****

*********

*******

You might also like