Variable & Data Type

 Variable & Data Type

1- Saving Values

In the first mission, we learned the basics of Python programming and performed a few arithmetical operations using Python. In this mission, we'll learn how to save values, and how to work with numerical and text data.

Instructions

Save the result of (42 - 11) * 22 to result.
Print result.


2. Variables


When we run the code result = 20, the value 20 is saved in the computer memory. The computer memory has many storage locations, and 20 is saved to one particular location.

The storage location to which we saved 20 has a unique identifier, and we can use it to access 20. The identifier is named result, and we named it that way when we ran the code result = 20.

Instructions

Store the value 15 in a variable named a_value.
Store the result of (25 - 7) * 17 to a variable named a_result.
Using the print() command, display the following:
The value stored in the a_value variable.
The result of adding 12 to the variable a_result.
The result of adding a_value to a_result.




3- Variable Names

We learned that we can choose different names for variables. However, the names we can use must comply with a number of syntax rules. For instance, naming a variable a result will output a syntax error because we're not allowed to use space characters in variable names.

We must use only letters, numbers, or underscores (we can't use apostrophes, hyphens, whitespace characters, etc.). Variable names cannot start with a number.

Instructions

In the code editor on the right, we attempted to store 34000 in a variable named old-income, and 40000 in a variable named new income. But both of these variable names cause syntax errors, so we commented-out the code.

Change the variable name old-income to old_income to prevent a syntax error.
Change the variable name new income to new_income to prevent a syntax error.
Remove the # from each line so that the code will run, then run the code.




4- Updating Variables

Instructions

Update the variable income by adding 6000 to its current value.
The variable income is already shown in the code editor on the right.
Print income.



5- Syntax Shortcuts

Notice that these operators (+=, -=, *=, /=, **=) can only be used to update a variable. This means the variable being updated must already store a value. In other words, the variable must already be defined. When we try to update a variable that we haven't defined, we get an error called NameError.

Instructions

Assign a value of 20 to a variable named variable_1.
Assign a value of 20 to a variable named variable_2.
Update the value of variable_2 by adding 10 to its current value. You can take advantage of the += operator.
Update the value of variable_1 by multiplying its current value by 4. You can take advantage of the *= operator.
Display variable_1 and variable_2 using print().


6- Integers & Floats

So far, we only worked with integers like 20, -3, 30, etc. We can also make computations with decimal numbers.

In mathematics, integers are not the same as decimal numbers, and Python acknowledges this difference. We can use the type() command to see the type of a value, and confirm that Python distinguishes between integers and decimal numbers.

In computer programming, values are classified into different types, or data types. The type of value offers the computer the required information about the way that value should be handled. Depending on the type, the computer will know how to store a value in memory, or what operations can and can't be performed on a value.

int and float values have different types, but we can mix the values together in arithmetical operations. So we're not limited, for instance, to add an int value only to another int value — we can add an int value to a float value.

Instructions

Assign the integer 10 to a variable named variable_1.
Assign the float 2.5 to a variable named variable_2.
Update the value of variable_1 by adding the float 6.5 to its current value. You can use the += operator.
Update the value of variable_2 by multiplying its current value by the integer 2. You can use the *= operator.
Display variable_1 and variable_2 using print().


7- Conversion Between Types


It's possible to convert a float to an integer and vice versa. To convert an integer to a float, we can use the float() command. To convert a float to an integer, we can use the int() command. Notice the int() command rounded 4.3 down to 4. int() will always round a float down, even if the number after the decimal point is greater than five.

If we want to round off a number, we can instead use the round() command, which has more flexibility and can also round up.Note that it's possible to combine commands. For instance, we can encompass a round() command within a print() command. This is useful in some cases — if we wrote three round() commands one after another, only the output of the last one would be displayed.

Note that running the round() command doesn't change the value stored by a variable unless we assign the rounded value back to the variable.

Instructions

Assign the value 13.9 to a variable named variable_a.
Assign the value 2.8 to a variable named variable_b.
Round variable_a using the round() command, and assign back the rounded value to variable_a.
Convert variable_b from a float to an integer using the int() command, and assign back the converted value to variable_b.
Display variable_a and variable_b using the print() command.


8- Strings

So far, we've only dealt with int and float values. But in data science, numbers are not the only type of data we work with. Data source: Mobile App Store data set (Ramanathan Perumal) We can see the data in columns track_name and currency are represented using text, not numbers. In Python, we can create text by enclosing a sequence of characters within quotation marks (" ").

Instructions

Assign the string Pandora - Music & Radio to a variable named app_name.
Assign the string 4.0 to a variable named average_rating. Make sure you don't mistake a string for a float.
Assign the string 1724546 to a variable named total_ratings. Make sure you don't mistake a string for an integer.
Assign the string free to a variable named price.
Display the app_name variable using print().


9- Escaping Special Characters

Sometimes we'll need to create strings with quotation marks inside.

Instructions

Assign the string Facebook's new motto is "move fast with stable infra." to a variable named motto.
Notice there's a . character at the end of Facebook's new motto is "move fast with stable infra." — you'll need to include the . character in your answer.
Display the variable motto using print() — displaying motto is required for answer checking.



10- String Operations

When we have two or more distinct strings, it's possible to link them together using the + operator. The process of linking two or more strings together is called concatenation. It's also possible to concatenate a string with one or more copies of itself using the * operator, followed by a number which specifies the number of times the string has to be multiplied.

Instructions

Assign the string Facebook's rating is to a variable named facebook.
Assign the float 3.5 to a variable named fb_rating.
Convert fb_rating from a float to a string using the str() command, and assign the converted value to a new variable named fb_rating_str.
Concatenate the strings stored in facebook and fb_rating_str to form the string Facebook's rating is 3.5.
Assign the concatenated string to a variable named fb.
You'll need to add a space character between Facebook's rating is and 3.5 to avoid ending up with the string Facebook's rating is3.5.
Display the fb variable using print() — this is required for answer checking.

















Komentar

Postingan populer dari blog ini

OSINT

LOCAL/REMOTE FILE INCLUSION

OSINT UTS