Solution:
>>> print("Happy birthday") Happy birthday >>> 17 + 35 * 2 87 >>> print(1, 2, 3, 4, 5, 6, 7, 8, 9) 1 2 3 4 5 6 7 8 9 >>>
Solution:
import sys print("*---------------------------------") print("|", "First name: ", sys.argv[1]) print("|", "Second name: ", sys.argv[2]) print("*---------------------------------")
Solution:
Open your editor.
Create a file named calculate_speed.py and save it.
On the first two lines, we'll declare our two variables for the distance in kilometers and the time in hours:
distance_in_km = 150 time_in_hours = 2
In the next two lines, we'll calculate the distances in miles and the distance in meters based on the distance in kilometers:
distance_in_mi = distance_in_km / 1.6 distance_in_mtrs = distance_in_km * 1000
We'll then calculate the time in seconds based on the time in hours:
time_in_seconds = time_in_hours * 3600
Next, we'll calculate the speed in kilometers per hour, the speed in miles per hour, and the speed in miles per second:
speed_in_kph = distance_in_km / time_in_hours speed_in_mph = distance_in_mi / time_in_hours speed_in_mps = distance_in_mtrs / time_in_seconds
Finally, we'll print out our results:
print("The speed in kilometers per hour is:", speed_in_kph) print("The speed in miles per hour is:", speed_in_mph) print("The speed in meters per second is:", speed_in_mps)
We can now save our script and run it by using the python calculate_speed.py command.
Solution:
Open your editor.
Create a file named circle.py and save it.
On the first two lines, we'll declare our constant, π (PI), and the radius of the circle:
PI = 3.14159 radius = 7
On the next two lines, we'll run our calculations:
area = PI * radius**2 circumference = 2 * PI * radius
The ** operator raises the value to the specified exponent; in this case, the exponent is 2.
Lastly, we'll display the results:
print("Circumference of the circle:", circumference) print("Area of the circle:", area)
We can now save our script and run it by using the python circle.py command.
Solution:
>>> if 5 > 2: ... print("Greater than") ... x = 5 ... print(x * 2) ... else: ... print("Less than") ... print(2)
Solution:
Open your editor.
Create a file named multiplication_table.py and save it.
On the first line, we'll add a docstring explaining what our file does. Then, we'll assign number to the user's input and cast it to an integer:
""" This script generates a multiplication table from 1 to 10 for any given number. """ number = int(input("Generate a multiplication table for: "))
Next, we'll print 20 underscores as the top border of the table:
print("_" * 20)
We'll then multiply our number with each number from 1 to 10 and print that out:
print("1:", number) print("2:", number * 2) print("3:", number * 3) print("4:", number * 4) print("5:", number * 5) print("6:", number * 6) print("7:", number * 7) print("8:", number * 8) print("9:", number * 9) print("10:", number * 10)
Finally, we'll print 20 underscores again for the bottom border of the table, as in step 4.
print("_" * 20)
Save the file and run it by using the python multiplication_table.py command.