Professional Documents
Culture Documents
Prog - 1
>>> '42' + 3
Traceback (most recent call last):
'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
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.')
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
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
>>>
not True
not not not not True
# (1)
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:
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)
name = input()
# (3)
print('Thank you!')
# (4)
Break statement
while True:
# (1)
name = input()
# (2)
break
# (4)
print('Thank you!')
# (5)
Continue Statement
while True:
print('Who are you?')
name = input()
if name != 'Joe':
continue
#(1)
#(2)
#(3)
if password == 'swordfish':
break
#(4)
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)
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:
response = input()
if response == 'exit':
sys.exit()