Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Introduction In these two LABs, we will work on Data Types and Selections. Follow the instructions of this manual and fill the question boxeS.

1. Introduction

In these two LABs, we will work on Data Types and Selections. Follow the instructions of this manual and fill the question boxeS.

To open the Python interpreter (black windows) on Windows,you must look for Python on the application menu and click on the element with this icon .

To open the IDLE (white window) on Windows, you must lookfor Python on the application menu and click on the elementwith this icon .

To take a screenshot on Windows, you must press print scr and paste (CTRL + V) the image in this document.

Here is an example of a screenshot:

You will know that a sentence is a line of code or example code because it is highlighted in yellow.

When you are required to provide the run-output of a program,you must take a screenshot showing the output of a run (red circle in the picture below) and the program itself (blue circle in the picture below).

LAB 1

2. Reserved keywords

Some variable names are not allowed because Python uses them with other special meanings (reserved keywords). In other cases,some variable names cannot be accepted because they containforbidden symbols or start by a number.

Try to create variables with the following names:

miles, Test, a+b, ba, 4#R, $4, #44, apps, if, elif, x, y, radius

For example:

miles=10

a+b=10

b-a=10

Then say whether the identifier is accepted or not as a variable name.

Identifier

Accepted Yes/No and reason:

miles

Test

a+b

ba

4#R

$4

#44

apps

if

elif

x

y

radius

3. Basic Operations

On the interpreter run the following operations:

What are doing the operators /, //, % and **?

Reply below:

Attach a screenshot of the results.

4. Augmented Operations

On the interpreter, create a variable a=10, run the following operations, and print the result after each line (use print(a)):

What are doing the operators *= and %=?

Reply below:

Attach a screenshot of the results.

5. Variables and types

Open a python interpreter and write the instructions in the following block list. Please note that the examples are indeed examples. Therefore, you should use different values doing your code, do not copy-paste, or you will get errors.

Create a string, e.g., my_string=Hello Jeddah. Print the variable.

Create a string that contains the text representing a number, e.g., number_string=22. Test the type of the variable, e.g., type(number_string). Print the variable.

Get a integer from the previous string, e.g.,my_integer=int(number_string). Test the type of the variable, e.g., type(my_integer). Print the variable.

Get a floating from the previous string, e.g.,my_floating=float(number_string). Test the type of the variable, e.g., type(my_floating). Print the variable.

Get a string from the previous floating number, e.g.,my_new_string=str(my_floating). Test the type of the variable, e.g., type(my_new_string). Print the variable.

Get an integer from a floating number, e.g., int(22.3). Print the variable.

Eval a string to get a number, e.g., my_eval=eval(23.8). Test the type of the variable, e.g., type(my_eval). Print the variable.

Eval a new string to get a number, e.g.,my_eval2=eval(23). Test the type of the variable, e.g.,type(my_eval2). Print the variable. The function eval gave a different type of variable. Say why.

Reply below:

Try to get an integer from the first string, e.g.,my_integer_fail=int(my_string). Then, say why the operation failed.

Reply below:

Try to print the integer you were trying to get from a string, e.g., print(my_integer_fail). It does not work. Explain why.

Reply below:

Create a Boolean variable with the value False, e.g.,my_bool=False. Print its value. Make a string from that variable with the str operator, e.g.,my_false=str(my_bool). Note that the string is not the same as the Boolean variable. Test this concept with:

my_bool == my_false

my_bool != my_false

To finalize this part, attach a screenshot of your computer.

6. Python is case sensitive

On the python interpreter, execute the following instructions:

Create a variable named numberofstudents, e.g. numberofstudents=30.

Create a variable named NumberOfStudents, e.g. NumberOfStudents=31.

Create a variable named number_of_students, e.g. number_of_students=32.

Print the previous three variables, e.g.:

o print(numberofstudents)

o print(NumberOfStudents)

o print(number_of_students)

Ask for the id of the previous variables, e.g.,

o id(numberofstudents)

o id(NumberOfStudents)

o id(number_of_students)

The id gives the unique number associated with the variable. So why are we getting three different ids?

Reply below:

7. Training on Python errors

On the terminal, type the following:

Type an instruction with wrong indentation, e.g., >>> print(Hello)

Please note the space highlighted in red before the >>>symbols.

This instruction gives an error. Say why:

Reply below:

Type an instruction with a missing ending quotation mark, e.g., print(Hello)

Repair the error by providing the corrected instruction:

Reply below:

Type an instruction with a missing ending parenthesis, e.g., print(Hello

Repair the error by providing the corrected instruction:

Reply below:

Type an instruction with an impossible result, e.g., print(1/0)

Try to understand and explain with your words this error:

Reply below:

Type a sum of integer numbers breaked in two lines, type the following:

my_sum = 1 + 2 + 3\

+ 4 + 5

Then, print the result. Try the same but in the second line remove the first +:

my_sum = 1 + 2 + 3\

4 + 5

Explain why you got an error:

Reply below:

8. Write a short program

Open the IDLE, and using your lectures slides, starting from lecture 4, write a program that asks the user a number as input(remember to use eval) and computes the square root (before you must import math and then use sqrt as math.sqrt(number)). Then, print the result of the square root.

Add the program below:

Test the program for positive, negative and zero numbers. Then, say why it does not work with negative numbers:

Reply below:

If the program uses the power operator ** instead of sqrt, it will also work with negative numbers. Try this change. And say what kind of result you got:

Reply below:

9

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Watson Watson

5th Edition

0471715360, 978-0471715368

More Books

Students also viewed these Databases questions

Question

Write a note on job design.

Answered: 1 week ago

Question

Compute the derivative of f(x)cos(-4/5x)

Answered: 1 week ago

Question

Discuss the process involved in selection.

Answered: 1 week ago

Question

Differentiate tan(7x+9x-2.5)

Answered: 1 week ago