01. Syntax and Variables
01. Syntax and Variables
Variables
Exercise 1 β Choose a variable name
For each piece of information, choose a suitable Python variable name.
- The hostname of a server
- The IP address of a server
- The number of connected users
- The temperature of a server room
- Whether a server is running
- The name of a network interface
Exercise 2 β Valid or invalid?
Which of the following are valid Python variable names?
server_nameserver-nameserver11serverServerNameserver name_serverserver_name_2classip_address
Exercise 3 β Create some variables
Write a script and create variables containing the following information:
- The hostname
webserver01 - The IP address
192.168.1.20 - The number of CPU cores:
8 - Whether the server is running:
True
Print all the information in a label-like way.
Exercise 4 β Spot the problem
What is wrong with the following code?
server-name = "web01"
ip address = "10.0.0.20"
1server = "server01"Rewrite the code so that it works.
Exercise 5 β Variable or value?
Look at the following code:
hostname = "server01"
port = 22For each item, indicate whether it is a variable name or a value:
hostname"server01"port22
Exercise 6 β Which code is better?
Both examples work. Which one would you prefer?
A
a = "web01"
b = "192.168.1.20"
c = 443B
hostname = "web01"
ip_address = "192.168.1.20"
https_port = 443Explain your choice.
Exercise 7 β A small mistake
A student wrote:
server_name = "web01"
server_ip = 192.168.1.20What is wrong with the second line?
Correct the code.
Data types
Exercise 1 β What type is it?
What type does each variable contain?
hostname = "web01"
port = 443
temperature = 21.5
online = TrueFor each variable, write down its type (on paper with a pen).
Then use the type() function and print() to check your answers.
Exercise 2 β Server calculations
A server has the following specifications:
ram = 32
used_ram = 19
cpus = 8Write Python code that prints:
- The amount of free RAM.
- The percentage of RAM that is currently used.
- The number of CPUs after adding 2 CPUs.
Your output should look something like:
Free RAM: 13 GB
RAM used: 59.375%
CPUs after upgrade: 10Use f-strings for the output.
Exercise 3 β Working with hostnames
Create the following variable:
hostname = " webserver01 "Write code that prints:
- The hostname without the spaces around it.
- The hostname in uppercase.
- The hostname in lowercase.
- The length of the hostname.
- The hostname with
"web"replaced by"app".
Use the appropriate string functions and operators.
Exercise 4 β Is this server suitable?
Consider the following variables:
os = "Linux"
ram = 16
online = TrueA server is suitable for a particular application when:
- it runs Linux,
- it has at least 8 GB of RAM,
- and it is online.
Create a Boolean expression that checks whether the server is suitable.
Print the result:
Server suitable: TrueThen change one of the variables so that the result becomes False.
Exercise 5 β Data from a network device
A network device provides the following information as strings:
device_name = "switch01"
port_count = "24"
uptime_days = "37"
temperature = "31.5"Convert the appropriate values to useful types.
Then print:
Switch: switch01
Ports: 24
Uptime: 37 days
Temperature: 31.5 CFinally, calculate how many hours the device has been running and print that value as well.
Hint: You will need casting and a mathematical operator.
Exercise 6 β Working with an IP address
Consider:
ip_address = "192.168.10.25"Write code that prints:
- The first character of the IP address.
- The last character of the IP address.
- Whether
"168"occurs in the IP address. - The length of the IP address.
Then print all this information using f-strings.
Exercise 7 β User input from a monitoring system
A monitoring system gives you these values:
cpu_load = "75"
disk_free = "42.5"
server_online = "True"All three values are strings.
Convert them to the appropriate types and print:
CPU load: 75%
Free disk space: 42.5 GB
Server online: TrueThen use a Boolean expression to check whether the server has:
- less than 80% CPU load,
- more than 20 GB free disk space,
- and is online.
Print the result.
Exercise 8 β String manipulation
A server administrator entered the following hostname:
hostname = " WEB-SERVER-01 "Create a cleaned-up version of the hostname that:
- has no spaces at the beginning or end;
- is written in lowercase;
- uses
_instead of-.
Print the result.
The expected output is:
web_server_01Exercise 9 β What will Python do?
What happens when you run each of these?
print(10 + 5)
print("10" + "5")
print(10 + "5")
print(10 * 5)
print("10" * 5)For each line:
- Write down the output, or
- Write down
Errorif Python produces an error.
Then explain why the results are different.
First admin script
Exercise 1 β Server information
Ask the user for:
- the server hostname
- the server IP address
- the SSH port
The user should be able to enter the information themselves.
Print the result in this format:
Server: web01
IP: 192.168.1.20
SSH: 22Use one print() statement with multiple parameters and sep.
Exercise 2 β Network address
Ask the user to enter:
- a network address, for example
192.168.10.0 - a subnet mask, for example
24
The subnet mask should be converted to an integer.
Print the result like this:
Network: 192.168.10.0/24Then print the following on one line, separated by ->:
Network -> 192.168.10.0 -> Mask -> 24Use multiple parameters with print() and sep.
Exercise 3 β Server startup message
Ask the user for:
- the hostname of the server
- the number of CPUs
- the amount of RAM in GB
Convert the number of CPUs and the amount of RAM to integers.
Print a startup message where each piece of information is printed separately, without automatically moving to a new line.
For example:
Starting server web01... CPUs: 8... RAM: 16 GB... DONE!Use:
input()- casting
print()with multiple parameterssepend
Do not use a single f-string for the complete message.
Exercise 4 β Server status
Write a program that asks the user for:
- the server hostname
- the server IP address
- the number of CPU cores
- the amount of RAM in GB
- whether the server is online (
TrueorFalse)
Convert the CPU cores, RAM, and online status to the appropriate types.
Print a status report similar to:
Server: web01
IP address: 192.168.1.20
CPU cores: 8
RAM: 16 GB
Online: TrueThen print whether the server is considered powerful.
A server is powerful when it has at least 8 CPU cores and at least 16 GB of RAM.
Use a Boolean expression to determine this.
Exercise 5 β Network device information
Write a program that asks the user for:
- the name of a network device
- its IP address
- the number of ports
- its temperature in degrees Celsius
The number of ports and temperature should be converted to numbers.
Print the information using multiple parameters and sep:
Device-switch01-IP-192.168.1.2-Ports-24-Temperature-32.5CThen print:
Temperature OK: TrueA temperature is OK when it is below 40Β°C.
Finally, print the device name in uppercase and lowercase.
Exercise 6 β Login information
Write a program that asks the user for:
- a username
- a password
- the number of login attempts
The username may contain spaces at the beginning or end. Remove these spaces and convert the username to lowercase.
Convert the number of login attempts to an integer.
Print:
Username: admin
Login attempts: 3Then check whether the login should be allowed.
The login is allowed when:
- the username is
"admin" - and the number of login attempts is less than 5.
Print:
Login allowed: TrueUse a Boolean expression for the check.
Exercise 7 β Server backup
Write a program that asks the user for:
- the hostname of a server
- the amount of data that needs to be backed up in GB
- the available backup space in GB
Convert the two sizes to numbers.
Print a message showing the server and the backup sizes.
Then determine whether there is enough space for the backup.
There is enough space when the available backup space is greater than or equal to the amount of data.
Print:
Backup server: backup01
Data to backup: 125 GB
Available space: 200 GB
Enough space: TrueAlso print the amount of space that will remain after the backup.
Use an f-string for the first report and multiple parameters with sep for a second line:
Backup -> backup01 -> OKUse end so that the words Checking backup... and DONE appear on the same line.
Exercise 8 β Mini server checker
Write a program that asks the user for:
- a server hostname
- a server IP address
- the operating system
- the number of CPU cores
- the amount of RAM in GB
- the server temperature
- whether the server is online
Clean up the hostname using strip() and lower().
Convert the CPU cores, RAM and temperature to numbers. Convert the online status to a Boolean.
Print a report containing all the information.
Then check the following:
A server is ready for production when:
- it is online;
- it has at least 4 CPU cores;
- it has at least 8 GB RAM;
- its temperature is below 70Β°C;
- and its operating system is either
"linux"or"windows".
Print:
Production ready: TrueFinally, print the hostname, IP address and operating system on one line, separated by |.