Skip to content
Snippets Groups Projects
Commit 59ad761e authored by Jöran Frey's avatar Jöran Frey
Browse files

Minor fix

parent 3f6a0e90
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:becc5c8b tags:
# Exercise 2: Applying previous knowledge to Python
%% Cell type:markdown id:d4b93396 tags:
## 1 The basics
%% Cell type:markdown id:424d40de tags:
### 1.1 print Function
Given a variable a with the value 20, write a line of code to print:
``The value of a is 20`` <br>
by combining the text and the variable in the print() function.
%% Cell type:code id:1a2fb499 tags:
``` python
```
%% Cell type:markdown id:ec7048ce tags:
### 1.2 print Function and basic operations
Write a Python program to add, subtract, multiply, and divide two numbers.
Print the result of all Operations
%% Cell type:code id:c918530d tags:
``` python
```
%% Output
15
5
50
2.0
%% Cell type:markdown id:fbe9dd35 tags:
### 1.3 Input from User
Write a program that asks the user for their name and greets them. <br>
> Use the Function ``input("text for user")`` to give a user input to a variable
%% Cell type:code id:a16c6da4 tags:
``` python
```
%% Output
Hello, haniball!
%% Cell type:markdown id:4cd048de tags:
### 1.2 type casting
Create variables with basic data types with the following names, which can store the corresponding value:
| Name | Value |
| ---- | ------------- |
| a | 30492 |
| b | 'X' |
| c | 87.432 |
| d | "Hello World" |
| e | True |
%% Cell type:code id:cb7b2da4 tags:
``` python
```
%% Cell type:markdown id:72ea9ad1 tags:
Run the following script to test if your solution is correct
%% Cell type:code id:28745df1 tags:
``` python
def check_types(a, b, c, d, e):
if isinstance(a, int) and isinstance(b, str) and isinstance(c, float) and isinstance(d, str) and isinstance(e, bool):
return "Your result is correct"
else:
return "There is an error"
result = check_types(a, b, c, d, e)
print(result)
```
%% Output
Your result is correct
%% Cell type:markdown id:cb931520 tags:
### 1.3 comparing variables
Write a program that compares whether a is greater than b and prints ``True`` or ``False``
%% Cell type:code id:d0f961d8 tags:
``` python
a = 2
b = 6
print(a > b)
```
%% Output
False
%% Cell type:markdown id:be4e3737 tags:
### 2 Control structures
%% Cell type:markdown id:a0122eba tags:
### 2.1 if and else statements
%% Cell type:markdown id:e9e1fa12 tags:
#### 2.1.1 even number
Write a program that checks a number and outputs whether it is even or odd.
%% Cell type:code id:73e37cc2 tags:
``` python
number = 4
```
%% Cell type:code id:273fc6d1 tags:
``` python
```
%% Output
Die Zahl ist gerade.
%% Cell type:markdown id:e2fc0c01 tags:
## 2.1.2 Simple Conditional
Write a program that checks a number from a user input to see whether it is positive, negative or zero.
%% Cell type:code id:811d885d tags:
``` python
```
%% Output
The number is positive
%% Cell type:markdown id:eb66c676 tags:
#### 2.1.3 User roles
Write a program that checks the role of a user and issues the corresponding authorizations.<br><br>
**Admin** has all authorizations <br>
**Editor** can edit content <br>
**User** can view content <br>
%% Cell type:code id:4ad521c7 tags:
``` python
role =input("what's your role")
```
%% Cell type:code id:d54646c1 tags:
``` python
```
%% Output
can view content
%% Cell type:markdown id:d79ce120 tags:
#### 2.1.4 Leap year check
Write a program that checks whether a year is a leap year. A year is a leap year if it is dividable by 4, but not by 100, unless it is also dividable by 400.
%% Cell type:code id:92455593 tags:
``` python
year = input("What year should be checked")
```
%% Cell type:code id:d7d6109b tags:
``` python
```
%% Output
2001 is not a leap year
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment