Python Practice I
Test drive PyCharm
Bioengineering ’22-’23
Cristina Gallego Fabrega
Universitat
Internacional
de Catalunya
- Open PyCharm
- Open a new project and name it
‘informatics_XX’ (XX= your initials, eg:
informatics_CG)
- Open a new python file and call it:
‘conversation_XX’
PyCharm layout
PyCharm layout
Exercise 1: “Hello World”
1) I come in peace, please take me to your leader
2) print: “Hello World”
3) print (“Hello World”)
4) #this is a comment for myself
5) #print(‘what could possibly go wrong?’)
6) print(“Hello Again World”)
Exercise 2: Integers
1) >> x = 6 4) >>> age_of_sara = ‘27’
>>> print(x) >>> age_of_dan = age_of_sara + 1
>>> print(age_of_dan)
2) >>> y = x * 7 5) >>> age = 23
>>> print(y) >>> age = 97
3) >>> y = 100 6) >>> print(type(age))
>>> print(y)
>>> print(x)
Exercise 2: Integers Correct the
mistake in task 4
1) >> x = 6 4) >>> age_of_sara = ‘27’
>>> print(x) >>> age_of_dan = age_of_sara + 1
>>> print(age_of_dan)
2) >>> y = x * 7 5) >>> age = 23
>>> print(y) >>> age = 97
3) >>> y = 100 6) >>> print(type(age))
>>> print(y)
>>> print(x)
What will be returned if you type
print(age)
Exercise 3: Integers & simple math
1) Print(5+2) 4) people = 15
Print(5*2) groups = 5
Print(5-2) calculate how many people per
Print(5**2) group using the variable
group_size
2) cakes = 6
cakes = 34
5) cake_per_group = cakes/groups
print(cakes) print(cakes_per_group)
print(type(cakes_per_group)
3) cakes = 34
cakes = 6
print(cakes)
- Why is this result different from
task 4
Exercise 4: Floats & simple math
1) height = 1.94 4) print(type(pi))
weight = 105.1
2) bmi = weight / (height*height) 5) print(type(‘pi’)
print(bmi)
print(type(bmi))
3) print(pi) 6) print(type(“pi”)
print(“pi”)
Exercise 5: Strings
1) message: 'And now for something completely different '
2) print(message*5) 5) first = “100”
print(message/5) second = “150”
print(first+second)
3) print(message[3:7]) 6) first = 'Test '
second = 3
4) first = 100 print(first * second)
second = 150
print(first+second) print(first == second)
Exercise 7: Variable Names and Keywods
Try to create this variables
- Do they work?
1) 25B = ''not my house”
2) more@ = 1000000
3) class = 'Informatics'
Exercise 8: User input
1) >>> my_age = input(‘what is your age?\n’)
2) >>> print(my_age + 15)
3) print(int(my_age) + 15)
Exercise 9: User input
1) Write a program that uses input to prompt a user for their name, and then
welcomes them:
Name: Maria
Greeting: Welcome Maria
name = input('What is your Name\n')
print('Welcome', name)
Exercise 9.1 and 9.2
9.1) Homework:
Write a program to prompt the user for hours and rate per hour to
compute gross pay.:
Enter Hours: 35
Enter Rate: 2.75
Pay: 96.25
9.2) Homework:
Write your own program prompting the user for some data and using a
conditional statement to do something with it
Exercise 10: Solving errors
1) Remember example 3 – Typed in Pycham
How can we solve such problems? If you get such an error occurs, your
script immediately stop!
question = “what is your age?\n”
my_age = input(question)
print(int(my_age) + 15)
what is your age?
(python waiting for user to type...)
It’s a secret
Traceback (most recent call last):
File "<input>", line 1, in <module>
ValueError: invalid literal for int() with base 10:
Try and Except
• The idea of try and except is that you guess an error may happen and you add
some statements in case of such errors
• Handling an exception with a try statement is called catching an exception
• A conditional execution structure built into Python to handle these types of
‘exceptions’
Exercise 10: Solving Errors
How to fix Example 3 using try..except. Answer once with a number and
another with ‘it’s secret’
question = “what is your age?\n”
my_age = input(question)
try:
age = int(my_age)
print(print(int(age)+15)
except:
print('typing error')
Exercise 11: If statement
Write a program to prompt for a score between 0.0 and 1.0. If the score is
out of range, print an error message. If the score is between 0.0 and 1.0,
print a grade using the following table.
Exercise 11: If statement
Write a program to prompt for a score between 0.0 and 1.0. If the score is
out of range, print an error message. If the score is between 0.0 and 1.0,
print a grade using the following table.
Run the program repeatedly as
shown below to test the
various different values for
input.
Exercise 11: If statement
11.1) Homework:
Rewrite your pay computation (from exercise 5.2) to give the employee
1.5 times the hourly rate for hours worked above 40 hours.
Enter Hours: 45
Enter Rate: 10
Pay: 475.0
11.2) Homework:
Rewrite your pay program using try and except so that your program
handles non-numeric input gracefully by printing a message.
Exercise 11: If statement
The following shows two executions of the program:
Enter Hours: 20
Enter Rate: nine
Error, please enter numeric input
Enter Hours: forty
Error, please enter numeric input