You are on page 1of 34

PYTHON PROGRAMS

Prog - 1
>>> '42' + 3
Traceback (most recent call last):

File "<pyshell#0>", line 1, in <module>

'42' + 3
TypeError: Can't convert 'int' object to str implicitly
>>>

Prog-2
>>> 2 + 2
4
>>> 2
2

Prog 3
>>> 2 + 3 * 6
20
>>> (2 + 3) * 6
30
>>> 48565878 * 578453
28093077826734
>>> 2 ** 8
256

>>> 23 / 7
3.2857142857142856
>>> 23 // 7
3
>>> 23 % 7
2
>>> 2 + 2
4
>>> (5 - 1) * ((7 + 1) / (3 - 1))
16

Prog 4
>>> 5 +
File "<stdin>", line 1

5+

^
SyntaxError: invalid syntax
>>> 42 + 5 + * 2
File "<stdin>", line 1

42 + 5 + * 2

^
SyntaxError: invalid syntax

Prog 5
>>> 'Alice' + 'Bob'
'AliceBob
>>> 'Alice' + 42
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
'Alice' + 42
TypeError: Can't convert 'int' object to str implicitly

>>> 'Alice' * 5
'AliceAliceAliceAliceAlice'
>>> 'Alice' * 'Bob'
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
'Alice' * 'Bob'
TypeError: can't multiply sequence by non-int of type 'str'
>>> 'Alice' * 5.0
Traceback (most recent call last):
File "<pyshell#33>", line 1, in <module>
'Alice' * 5.0
TypeError: can't multiply sequence by non-int of type 'float'

Prog 6
For example, enter the following into the interactive shell:
>>> spam = 40
>>> spam
40
>>> eggs = 2
>>> spam + eggs
42
>>> spam + eggs + spam
82
>>> spam = spam + 2
>>> spam
42

overwriting the variable.


>>> spam = 'Hello'
>>> spam
'Hello'
>>> spam = 'Goodbye'
>>> spam
'Goodbye'

Prog 7
# This program says hello and asks for my name.
print('Hello world!')
print('What is your name?') # ask for their name
myName = input()
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.')

Output For Prog 7


Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600
64 bit
(AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART
================================
>>>
Hello world!
What is your name?
Al
It is good to meet you, Al
The length of your name is:
2
What is your age?
4
You will be 5 in a year
>>>

Prog 8
>>> str(0)
'0'
>>> str(-3.14)
'-3.14'
>>> int('42')
42
>>> int('-99')
-99
>>> int(1.25)
1
>>> int(1.99)
1
>>> float('3.14')
3.14
>>> float(10)
10.0

>>> spam = input()


101
>>> spam
'101
The value stored insidespamisnt the integer101but the string'101'. If you want to
do math using the value inspam
>>> spam = int(spam)
>>> spam
101
Now you should be able to treat the spam variable as an integer instead of a string.
>>> spam * 10 / 5
202.0,
Use theint()function to get the integer form ofspamand then store this as the new
value inspam.

Note that if you pass a value to int() that it cannot evaluate as an integer, Python
will display an error message.
>>> int('99.99')
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
int('99.99')
ValueError: invalid literal for int() with base 10: '99.99
>>> int('twelve')
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
int('twelve')
ValueError: invalid literal for int() with base 10: 'twelve'

Prog 9
>>> spam = True
# (1)
>>> spam
True
>>> true
# (2)
Traceback (most recent call last):
File "python", line 1, in
NameError: name 'true' is not defined
>>> True = 2 + 2 # (3)
Traceback (most recent call last):
File "python", line 1
SyntaxError: can't assign to keyword
>>>

Prog 10
>>> 42 == 42
True
>>> 42 == 99
False
>>> 2 != 3 True
>>> 2 != 2
False
>>>
>>> 'hello' == 'hello'
True
>>> 'hello' == 'Hello'
False
>>> 'dog' != 'cat'
True
>>> True == True
True
>>> True != False
True
>>> 42 == 42.0
True
>>> 42 == '42'
False
>>>

Prog 11
>>>
True
>>>
False
>>>
True
>>>
False
>>>

True and True


True and False
False or True
False or False

Mixing Boolean Operators


>>>
False
>>>
True
>>>
True
>>>
False
>>>
True
>>>
True
>>>

not True
not not not not True

# (1)

(4 < 5) and (5 < 6)


(4 < 5) and (9 < 6)
(1 == 2) or (2 == 2)
2 + 2 == 4 and not 2 + 2 == 5 and 2 * 2 == 2 + 2

Prog 12
name = 'Mary'
password = 'swordfish'
if name == 'Mary':

print('Hello Mary')

if password == 'swordfish':

print('Access granted.')

else:

print('Wrong password.')

Output of Prog 12
Hello Mary
Access granted.

Prog 13
name = 'Dracula'
age = 4000
if name == 'Alice':

print('Hi, Alice.')
elif age < 12:

print('You are not Alice, kiddo.')


elif age > 2000:

print('Unlike you, Alice is not an undead, immortal vampire.')


elif age > 100:

print('You are not Alice, grannie.')

Output Of Prog 13
Unlike you, Alice is not an undead, immortal vampire.

Prog 14
spam = 0
while spam < 5:

print('Hello, world.')

spam = spam + 1

Output of Prog 14
Hello,
Hello,
Hello,
Hello,
Hello,

world.
world.
world.
world.
world.

name = ''
# (1)
while name != 'your name':
# (2)

print('Please type your name.')

name = input()
# (3)
print('Thank you!')
# (4)

#can run into an infinite loop

Break statement
while True:
# (1)

print('Please type your name.')

name = input()
# (2)

if name == 'your name':


# (3)

break
# (4)
print('Thank you!')
# (5)

Continue Statement
while True:
print('Who are you?')
name = input()
if name != 'Joe':
continue

#(1)
#(2)

print('Hello, Joe. What is the password? (It is a fish.)')


password = input()

#(3)

if password == 'swordfish':
break

#(4)

print('Access granted.') #(5)

Prog 15
name =
while not name: #(1)
print('Enter your name:')
name = input()
print('How many guests will you have?')
numOfGuests = int(input())
if numOfGuests:

#(2)

print('Be sure to have enough room for all your


guests.')#(3)

Prog 16
print('My name is')
for i in range(5):
print('Jimmy Five Times (' + str(i) + ')')

Output Of Prog 16
My name is
Jimmy Five Times
Jimmy Five Times
Jimmy Five Times
Jimmy Five Times
Jimmy Five Times

(0)
(1)
(2)
(3)
(4)

Prog 17
for i in range(5, -1, -1):
print(i)

Prog 18
import random
for i in range(5):
print(random.randint(1, 10))

Prog 19
import sys
while True:

print('Type exit to exit.')

response = input()

if response == 'exit':

sys.exit()

print('You typed ' + response + '.')

You might also like