Python Lab1
Python Lab1
Task 1:
In Python's Shell, write print('Hello world') and then execute (press Enter). Then write your
first script containing the same statement. Select File->New file from the Menu. Name your
script e.g. script1.py.
Run it up from Shell. What happens when a script is running from Windows? Complete the
script by adding the instruction input('Press enter to continue ...') at the end of the script.
Check what is happen.
1
Assoc. Prof. Krzysztof Małecki
Python Programming Language, Faculty of Computer Science and Information
Technology, West Pomeranian University of Technology, Szczecin, Poland
Task 2:
In Shell make the calculation: the human heart beats 72 times a minute on average. How
many beats does the 70-year-old's heart have?
Task 3:
Write a script that draws a square on the screen (unfortunately visible as a rectangle). Use
instruction print:
*****
* *
* *
* *
*****
Task 4:
Knowing that 5 spiders eat 5 houseflies in 5 hours, write a script that counts how many
houseflies will be eaten by 100 spiders in 100 hours?
Task 5:
Find the error in the script below (you can copy the script and try to run it). There are 2
errors :)
#definitionOfVariables
daysOfWorkPerMonth = 20
monthsInYear = 12
vacation = 26
yearsOfWOrk = 40
#result
print((daysOfWorkPermonth * monthsInYear - Vacation)*yearsOfWOrk)
Task 6:
The instruction print() – observe how particular lines work (rewrite or paste the code):
print('TVP1','TVN','Polsat','BBC')
print('TVP1','TVN','Polsat','BBC', sep='\n')
print('TVP1','TVN','Polsat','BBC', sep='\t')
print('TVP1','TVN','Polsat','BBC', sep=';')
print('TVP1','TVN','Polsat','BBC', sep='-')
print('I like computers ','TVP1','TVN','Polsat','BBC', sep=' but better is ')
#variables definition
ProgramName = 'BBC'
Item = 'News'
Time = '18:00'
print('I like watching',Item,'at',Time,'on',ProgramName,'.')
print('I like watching ',Item,' at ',Time,' on ',ProgramName,'.', sep='')
print('\u03A3') # the sum character as a Unicode character code
print('this is a backslash: \\') #backslash has a particular meaning...
Task 7:
Write a script that calculates the circle area and circle circumference, the area of the
rectangle and the area of the trapezoid. Create variables in the program, e.g. ValuePi
(assign it a value of 3.14), CircleRadius (assign it a value of 5), AreaCircle (assign it a value
2
Assoc. Prof. Krzysztof Małecki
Python Programming Language, Faculty of Computer Science and Information
Technology, West Pomeranian University of Technology, Szczecin, Poland
using the proper formula for the area of a circle). Be careful with the name of all variables.
Display the results with the print command. Before each solution, add an appropriate
comment (which is displayed on the screen so that there is more than just a "blank" value
on the screen. Make comments in the script (this is good code writing practice).
Task 8:
Write a script using print commands, which put on the screen ascii art presented below:
(\(\
(-.-)
O_(")(")
Task 9:
Check in the Shell (input and check each line separately):
somethingLikeNumber='1000'
print(somethingLikeNumber)
somethingLikeNumber+1
int(somethingLikeNumber)+1 # the conversion of character type to integer
somethingLikeNumber+str(1) # the conversion of integer to character type
type(somethingLikeNumber) # type check
type((int(somethingLikeNumber))+1) # checking for type projection to integer
Task 10:
Check in the Shell (input and check each line separately):
five=5
three=3
type(five)
five/three
type(five/three)
import sys
sys.maxsize #the maximum value of int in Python
veryBigValue=999999999999999999999999999999999999999999
veryBigValue
type(veryBigValue) #and what is going here?????
veryBigValue+1
veryBigValue+1/2 #and here?
type(veryBigValue+1/2)
3
Assoc. Prof. Krzysztof Małecki