Posts

Showing posts from September, 2022

Exercise:08 python variable and its types

Image
  Type conversion Using the  +  operator to paste together two strings can be very useful in building custom messages. Suppose, for example, that you've calculated the return of your investment and want to summarize the results in a string. Assuming the integer  savings  and float  result  are defined, you can try something like this: print("I started with $" + savings + " and now have $" + result + ". Awesome!") This will not work, though, as you cannot simply sum strings and integers/floats. To fix the error, you'll need to explicitly convert the types of your variables. More specifically, you'll need  str() , to convert a value into a string.  str(savings) , for example, will convert the integer  savings  to a string. Similar functions such as  int() ,  float()  and  bool()  will help you convert Python values into any type. Instructions 100 XP Hit  Run Code  to run the code. Try to understand t...

Exercise:07 python variable and its types

Image
  Operations with other types Usman mentioned that different types behave differently in Python. When you sum two strings, for example, you'll get different behavior than when you sum two integers or two booleans. In the script some variables with different types have already been created. It's up to you to use them. Instructions 100 XP Calculate the product of  savings  and  growth_multiplier . Store the result in  year1 . What do you think the resulting type will be? Find out by printing out the type of  year1 . Calculate the sum of  desc  and  desc  and store the result in a new variable  doubledesc . Print out  doubledesc . Did you expect this?

Exercise:06 Python variable and Its types

Image
  Other variable types In the previous exercise, you worked with two Python data types: int , or integer: a number without a fractional part.  savings , with the value  100 , is an example of an integer. float , or floating point: a number that has both an integer and fractional part, separated by a point.  growth_multiplier , with the value  1.1 , is an example of a float. Next to numerical data types, there are two other very common data types: str , or string: a type to represent text. You can use single or double quotes to build a string. bool , or boolean: a type to represent logical values. Can only be  True  or  False  (the capitalization is important!). Instructions 100 XP Create a new string,  desc , with the value  "compound interest" . Create a new boolean,  profitable , with the value  True . Question:01: Guess the type To find out the type of a value or a variable that refers to that value, you can use the...

Exercise:05 Python variable and its Types

Image
  Calculations with variables Remember how you calculated the money you ended up with after 7 years of investing $100? You did something like this: 100 * 1.1 ** 7 Instead of calculating with the actual values, you can use variables instead. The  savings  variable you've created in the previous exercise represents the $100 you started with. It's up to you to create a new variable to represent  1.1  and then redo the calculations! Instructions 100 XP Create a variable  growth_multiplier , equal to  1.1 . Create a variable,  result , equal to the amount of money you saved after  7  years. Print out the value of  result .

Exercise:04 Python variable and Its types

Image
  Variable Assignment In Python, a variable allows you to refer to a value with a name. To create a variable use  = , like this example: x = 5 You can now use the name of this variable,  x , instead of the actual value,  5 . Remember,  =  in Python means  assignment , it doesn't test equality! Instructions 100 XP Create a variable  savings  with the value 100. Check out this variable by typing  print(savings)  in the script.

Python Variables and its Types

Image
 Python Variables and its Types 1. Variables and Types Well done and welcome back! It's clear that Python is a great calculator. If you want to do more complex calculations though, you will want to "save" values while you're coding along. ` 2. Variable You can do this by defining a variable, with a specific, case-sensitive name. Once you create (or declare) such a variable, you can later call up its value by typing the variable name. Suppose you measure your height and weight, in metric units: you are 1-point-79 meters tall and weigh 68-point-7 kilograms. You can assign these values to two variables, named height and weight, with an equals sign: If you now type the name of the variable, height, Python looks for the variable name, retrieves its value, and prints it out. 3. Calculate BMI Let's now calculate the Body Mass Index, or BMI, which is calculated as follows, with weight in kilograms and height in meters. You can do this with the actual values, but you can j...

Exercise:03 Introduction to python

Image
  Python as a calculator Python is perfectly suited to do basic calculations. Apart from addition, subtraction, multiplication, and division, there is also support for more advanced operations such as: Exponentiation:  ** . This operator raises the number to its left to the power of the number to its right. For example  4**2  will give  16 . Modulo:  % . This operator returns the remainder of the division of the number to the left by the number on its right. For example  18 % 7  equals  4 . The code in the script gives some examples. Instructions 100 XP Suppose you have $100, which you can invest with a 10% return each year. After one year, it's 100*1.1=110 100 × 1.1 = 110  dollars, and after two years it's 100*1.1*1.1=121  100 × 1.1 × 1.1 = 121 . Add code to calculate how much money you end up with after 7 years, and print the result.

Exercise 2 for Introduction to Python

Image
  How to write comments in python? You can add  comments  to your Python scripts. Comments are important to make sure that you and others can understand what your code is about. To add comments to your Python script, you can use the  #  tag. These comments are not run as Python code, so they will not influence your result. As an example, take the comment in the editor,  # Division ; it is completely ignored during execution. Instructions 100 XP Above the  print(7 + 10) , add the comment # Addition

Exercise1 for Introduction to python

Image
  The Python Interface You can also use the IPython Shell interactively by simply typing commands and hitting Enter. When you work in the shell directly, your code will not be checked for correctness so it is a great way to experiment. Instructions 100 XP Experiment in the IPython Shell; type  5 / 8 , for example. Add another line of code to the Python script on the top-right (not in the Shell):  print(7 + 10) . Question:01 When to use Python? Python is a pretty versatile language. For which applications can you use Python? Instructions 50 XP Possible Answers You want to do some quick calculations. For your new business, you want to develop a database-driven website. Your boss asks you to clean and analyze the results of the latest satisfaction survey. All of the above.