Skip to content
Snippets Groups Projects
Commit c8dca795 authored by William Dos Santos's avatar William Dos Santos
Browse files

finished exercise 2

parent 6872a7cf
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 is a variable "a" with the value 20, write a line of code that prints:
``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
a = 20
if a==20:
print("The value of a is 20")
print("The value of a is",a) #das wäre das Ziel der Aufgabe
```
%% Output
The value of a is 20
The value of a is 20
%% Cell type:markdown id:ec7048ce tags:
### 1.2 print Function and basic operations
Write a Python program to add, subtract, multiply, and divide two variables a and b.
Print the result of all Operations
%% Cell type:code id:c918530d tags:
``` python
a = 10
b = 5
print(a+b) #Addition
print(a-b) #Subtraktion
print(a*b) #Multiplikation
print(a/b) #Division
```
%% 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
name=input("What's your name?")
print("Hello, " , name + ". Nice to meet you!")
print("Hello, " , name , ". Nice to meet you!") # Wenn Komma verwendet wird, wird am Schluss einen Leerschlag erzeugt.
print("Hello, " + name + ". Nice to meet you!") #Nur wenn keine Zahlen verwendet werden, sonst ,+ benutzen.
```
%% Output
Hello, William. Nice to meet you!
Hello, William . Nice to meet you!
Hello, William. Nice to meet you!
%% 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
a=30492
b='X'
c=87.432
d="Hello World"
e=True
```
%% 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 = 10
b = 5
a > b
```
%% Output
True
%% 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 = 3
if number % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
```
%% Cell type:code id:273fc6d1 tags:
%% Output
``` python
```
The number is odd.
%% 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
number = float(input("Which number?"))
if number > 0:
print("Positive")
elif number < 0:
print("Negative")
else:
print("Zero")
```
%% Output
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")
role = input("what's your role?")
if role == "admin":
print("Admin has all authorizations.")
elif role == "editor":
print("Editor can edit content. ")
elif role == "user":
print("User can view content.")
else:
print("Unknown user cannot access.")
```
%% Cell type:code id:d54646c1 tags:
%% Output
``` python
```
Admin has all authorizations.
%% 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:
year = int(input("What year should be checked?"))
``` python
if (year % 4 == 0 and (year % 100 != 0 or year % 400 == 0 )):
print(" ", year,"is a leap year.")
else:
print(" ", year,"is not a leap year.")
```
%% Output
2001 is not a leap year
2022 is not a leap year.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment