Vous êtes sur la page 1sur 8

Python Code http://localhost:8888/nbconvert/html/Python%20Code.ipynb?downlo...

In [8]: #user input


print('Please enter some text:')
x = input()
print('Text entered:', x)
print('Type:', type(x))

Please enter some text:


ty
Text entered: ty
Type: <class 'str'>

In [5]: print('Please enter an integer value:')


x = input()
print('Please enter another integer value:')
y = input()
num1 = int(x)
num2 = int(y)
print(num1, '+', num2, '=', num1 + num2)

Please enter an integer value:


3
Please enter another integer value:
56
3 + 56 = 59

In [9]: x = input('Please enter an integer value: ')


y = input('Please enter another integer value: ')
num1 = int(x)
num2 = int(y)
print(num1, '+', num2, '=', num1 + num2)

Please enter an integer value: 45


Please enter another integer value: 56
45 + 56 = 101

In [10]: num1 = int(input('Please enter an integer value: '))


num2 = int(input('Please enter another integer value: '))
print(num1, '+', num2, '=', num1 + num2)

Please enter an integer value: 43


Please enter another integer value: 34
43 + 34 = 77

1 of 8 9/29/2022, 8:48 AM
Python Code http://localhost:8888/nbconvert/html/Python%20Code.ipynb?downlo...

In [20]: #The eval function dynamically translates the text provided by the user i
nto an executable form that the program can process.
x1 = eval(input('Entry x1? '))
print('x1 =', x1, ' type:', type(x1))

x2 = eval(input('Entry x2? '))


print('x2 =', x2, ' type:', type(x2))

x3 = eval(input('Entry x3? '))


print('x3 =', x3, ' type:', type(x3))

Entry x1? 67
x1 = 67 type: <class 'int'>
Entry x2? 7.8
x2 = 7.8 type: <class 'float'>
Entry x3? "sayani"
x3 = sayani type: <class 'str'>

In [16]: num1, num2 = eval(input('Please enter number 1, number 2: '))


print(num1, '+', num2, '=', num1 + num2)

Please enter number 1, number 2: 89,89


89 + 89 = 178

print(eval(input()))

In [18]: print(eval(input()))

5+4
9

In [21]: # Assign some Boolean variables


a = True
b = False
print('a =', a, ' b =', b)
# Reassign a
a = False;
print('a =', a, ' b =', b)

a = True b = False
a = False b = False

In [28]: # Get two integers from the user


dividend, divisor = eval(input('Please enter two numbers to divide: '))

# If possible, divide them and report the result


if divisor != 0:
quotient = dividend/divisor
print(dividend, '/', divisor, "=", quotient)
print('Program finished')

Please enter two numbers to divide: 56,2


56 / 2 = 28.0
Program finished

2 of 8 9/29/2022, 8:48 AM
Python Code http://localhost:8888/nbconvert/html/Python%20Code.ipynb?downlo...

In [39]: # Get two integers from the user


dividend, divisor = eval(input('Please enter two numbers to divide: '))

# If possible, divide them and report the result


if divisor != 0:
print(dividend, '/', divisor, "=", dividend/divisor)
else:
print('Division by zero is not allowed')

Please enter two numbers to divide: 64,8


64 / 8 = 8.0

In [40]: value = eval(input("Please enter an integer value in the range 0...10:


"))
if (value >= 0): # First check
if (value <= 10): # Second check
print("In range")
print("Done")

Please enter an integer value in the range 0...10: 6


In range
Done

In [47]: value = eval(input("Please enter an integer value in the range 0...10:


"))
if (value >= 0): # First check
if (value <= 10): # Second check
print(value, "is in range")
else:
print(value, "is too large")
else:
print(value, "is too small")
print("Done")

Please enter an integer value in the range 0...10: -9


-9 is too small
Done

3 of 8 9/29/2022, 8:48 AM
Python Code http://localhost:8888/nbconvert/html/Python%20Code.ipynb?downlo...

In [52]: value = eval(input("Please enter an integer in the range 0...5: "))

if value < 0:
print("Too small")

elif value == 0:
print("zero")

elif value == 1:
print("one")

elif value == 2:
print("two")

elif value == 3:
print("three")

elif value == 4:
print("four")

elif value == 5:
print("five")

else:
print("Too large")
print("Done")

Please enter an integer in the range 0...5: 6


Too large
Done

In [53]: # WHILE STATEMENT


count = 1 # Initialize counter
while count <= 5:
print(count) # Display counter
count += 1 # Increment counter

1
2
3
4
5

In [2]: for n in range(21, 0, -3):


print(n, '', end='')

21 18 15 12 9 6 3

4 of 8 9/29/2022, 8:48 AM
Python Code http://localhost:8888/nbconvert/html/Python%20Code.ipynb?downlo...

In [5]: sum = 0 # Initialize sum


for i in range(1, 10):
sum += i
print(sum)

1
3
6
10
15
21
28
36
45

In [6]: sum = 0 # Initialize sum


for i in range(1, 10,2):
sum += i
print(sum)

1
4
9
16
25

In [20]: # Print a multiplication table to 10 x 10


# Print column heading
print(" 1 2 3 4 5 6 7 8 9 10")
print(" +----------------------------------------")
for row in range(1, 6): # 1 <= row <= 10, table has 10 rows
if row < 10: # Need to add space?
print(" ", end="")
print(row, "| ", end="") # Print heading for this row.
for column in range(1, 6): # Table has 10 columns.
product = row*column; # Compute product
if product < 100: # Need to add space?
print(end=" ")
if product < 10: # Need to add another space?
print(end=" ")
print(product, end=" ") # Display product
print()

1 2 3 4 5 6 7 8 9 10
+----------------------------------------
1 | 1 2 3 4 5 2 | 2 4 6 8 10 3 | 3 6 9 12
15 4 | 4 8 12 16 20 5 | 5 10 15 20 25

5 of 8 9/29/2022, 8:48 AM
Python Code http://localhost:8888/nbconvert/html/Python%20Code.ipynb?downlo...

In [22]: entry = 0 # Ensure the loop is entered


sum = 0 # Initialize sum

# Request input from the user


print("Enter numbers to sum, negative number ends list:")

while True: # Loop forever


entry = eval(input()) # Get the value
if entry < 0: # Is number negative number?
break # If so, exit the loop
sum += entry # Add entry to running sum
print("Sum =", sum) # Display the sum

Enter numbers to sum, negative number ends list:


8
9
9
7
6
-1
Sum = 39

In [24]: sum = 0
done = False;
while not done:
val = eval(input("Enter positive integer (999 quits):"))
if val < 0:
print("Negative value", val, "ignored")
continue; # Skip rest of body for this iteration
if val != 999:
print("Tallying", val)
sum += val
else:
done = (val == 999); # 999 entry exits loop
print("sum =", sum)

Enter positive integer (999 quits):8


Tallying 8
Enter positive integer (999 quits):98
Tallying 98
Enter positive integer (999 quits):-5
Negative value -5 ignored
Enter positive integer (999 quits):999
sum = 106

In [27]: a = 0
while a < 100:
print('*', end='')
a += 1
print()

************************************************************************
****************************

6 of 8 9/29/2022, 8:48 AM
Python Code http://localhost:8888/nbconvert/html/Python%20Code.ipynb?downlo...

In [28]: from math import sqrt

# Get value from the user


num = eval(input("Enter number: "))

# Compute the square root


root = sqrt(num);

# Report result
print("Square root of", num, "=", root)

Enter number: 8
Square root of 8 = 2.8284271247461903

In [29]: from time import clock

print("Enter your name: ", end="")


start_time = clock()
name = input()
elapsed = clock() - start_time
print(name, "it took you", elapsed, "seconds to respond")

Enter your name: sayani


sayani it took you 2.9382425000000003 seconds to respond

In [41]: # Start of program


print("This program adds together two integers.")

# Print a message to prompt the user for input


def prompt():
print("Please enter an integer value: ", end="")

prompt() # Call the function


value1 = int(input())
prompt() # Call the function again
value2 = int(input())
sum = value1 + value2;
print(value1, "+", value2, "=", sum)

This program adds together two integers.


Please enter an integer value: 7
Please enter an integer value: 8
7 + 8 = 15

7 of 8 9/29/2022, 8:48 AM
Python Code http://localhost:8888/nbconvert/html/Python%20Code.ipynb?downlo...

In [43]: # Count to ten and print each number on its own line
def count_to_10():
for i in range(1, 11):
print(i)

print("Going to count to ten . . .")


count_to_10()
print("Going to count to ten again. . .")
count_to_10()

Going to count to ten . . .


1
2
3
4
5
6
7
8
9
10
Going to count to ten again. . .
1
2
3
4
5
6
7
8
9
10

In [44]: def rev(lst):


return [] if len(lst) == 0 else rev(lst[1:]) + lst[0:1]

print(rev([1, 2, 3, 4, 5, 6, 7]))

[7, 6, 5, 4, 3, 2, 1]

8 of 8 9/29/2022, 8:48 AM

Vous aimerez peut-être aussi