UWPCE-PythonCert / PythonCertDevel

Development Repo for the Certificate Program
https://uwpce-pythoncert.github.io/PythonCertDevel/index.html
Other
7 stars 15 forks source link

updates to string formatting exercise #84

Closed ghost closed 6 years ago

ghost commented 6 years ago

Updates made and built locally successfully. Not sure if this is now over the top, or needs more guidance but I tried to introduce some challenges.

Looks like from a quick scan through email the delivery method is changing, so I will take a look tomorrow, and contact Liz (et al).

PythonCHB commented 6 years ago

yeah, still confused about what's going to be published where / how, but this repo is the master copy for now.

PythonCHB commented 6 years ago

We're going to need to be careful about links back to Canvas, like:

https://canvas.uw.edu/courses/1200526/assignments/3970003?module_item_id=7963708

I don't think those will stay static, and there will be multiple "instances" of the course.

And that link goes to the list exercise --not sure if that's intended.

ghost commented 6 years ago

Great suggestions, thank you. Changes made and built successfully. Ready for another merge attempt!


From: Christopher H.Barker, PhD notifications@github.com Sent: Saturday, December 30, 2017 7:26 PM To: UWPCE-PythonCert/PythonCertDevel Cc: Andy Miles; Author Subject: Re: [UWPCE-PythonCert/PythonCertDevel] updates to string formatting exercise (#84)

@PythonCHB requested changes on this pull request.

looks good, maybe put in those extra couple examples, and we're done.


In source/exercises/string_formatting.rsthttps://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FUWPCE-PythonCert%2FPythonCertDevel%2Fpull%2F84%23discussion_r159133021&data=02%7C01%7C%7Ca845c9cf4a664edbed7f08d54ffe4299%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636502875806675749&sdata=rJdYDA5Jm4E8B2UerSYi8taji8uNbd5CaGDA5Ytc9OM%3D&reserved=0:


+f-strings are new to Python (version 3.6), but are very powerful and efficient. This means they are worth understanding and using. And this is made easier than it might be because they use the same, familiar formatting language that is conventionally used in Python (in .format()). + +So in this exercise we are going to specifically use f-strings. + +Here's the simplest example, just to show the basic syntax:

I'm not sure we should have an example if an f-string with no formatting -- kinda makes you wonder what the difference is -- and the answer is "nothing" :-)

So let's leave this out and go straight to the example with {name}


In source/exercises/string_formatting.rsthttps://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FUWPCE-PythonCert%2FPythonCertDevel%2Fpull%2F84%23discussion_r159133081&data=02%7C01%7C%7Ca845c9cf4a664edbed7f08d54ffe4299%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636502875806675749&sdata=d9HyyeriCtHBIeZtJD1XKrCfb1nYsyqwyQKaGDNUc2c%3D&reserved=0:

+ +Here's the simplest example, just to show the basic syntax: + +.. code-block:: ipython +

  • In [1]: f'a simple f-string'
  • Out[1]: 'a simple f-string'
  • +And here's an example that shows how you can use available variables in a f-string:

  • +.. code-block:: ipython

  • In [2]: name = 'Andy'
  • In [3]: f'Your name is {name}'
  • Out[3]: 'Your name is Andy'

Now let's add an example of evaluating an expression in an f-string:

In addition to referencing variables in the local scope, f-strings can evaluate simple expressions in line like so:

In [5]: f"Your name is {name.upper()}" Out[5]: 'Your name is ANDY'

In [6]: name = "andy"

In [7]: f"Your name is {name.upper()}" Out[7]: 'Your name is ANDY'

or

In [8]: a = 5

In [9]: b = 10

In [10]: f"The sum is: {a+b}" Out[10]: 'The sum is: 15'


In source/exercises/string_formatting.rsthttps://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FUWPCE-PythonCert%2FPythonCertDevel%2Fpull%2F84%23discussion_r159133083&data=02%7C01%7C%7Ca845c9cf4a664edbed7f08d54ffe4299%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636502875806675749&sdata=LaFuG9wWgTkcTqOO%2F1WTGi7TflsNo27oKmwL9unNmag%3D&reserved=0:

+ +.. code-block:: ipython +

  • In [2]: name = 'Andy'
  • In [3]: f'Your name is {name}'
  • Out[3]: 'Your name is Andy'
  • +* So here's a task for you. Given a four element list:

  • ['oranges', 1.3, 'lemons', 1.1]
  • +* Write an f-string that will display:

  • The weight of an orange is 1.3 and the weight of a lemon is 1.1
  • +* Now see if you can change the f-string so that it displays the names of the fruit in upper case, and the weight 20% higher (that is 1.2 times higher).

nice!


In source/exercises/string_formatting.rsthttps://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FUWPCE-PythonCert%2FPythonCertDevel%2Fpull%2F84%23discussion_r159133093&data=02%7C01%7C%7Ca845c9cf4a664edbed7f08d54ffe4299%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636502875806675749&sdata=KbthSUS7PnxVq3MqZsP51IlfElkxRvBnoiJv84W42Us%3D&reserved=0:

+Often it's convenient to display data in columns. String formatting helps to make this straightforward. + +Suppose you'd like to display something like: +

  • 'First $99.01 Second $88.09 '
  • +One way to do that is:

  • +.. code-block:: ipython

  • '{:20}{:10}{:20}{:8}'.format('First', '$99.01', 'Second', '$88.09')
  • +In this simple example everything aligns nicely. But that will not be the case when the numbers to the left of the decimal place vary. +Then you will need to use alignment specifiers. Do some research on this using the links below. Then:

  • +* Write some Python code to print a table of several rows, each with a name, an age and a cost. Make sure some of the costs are in the hunderds and thousands to test your alignment specifiers.

typo: hunderds => hundreds

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FUWPCE-PythonCert%2FPythonCertDevel%2Fpull%2F84%23pullrequestreview-86039880&data=02%7C01%7C%7Ca845c9cf4a664edbed7f08d54ffe4299%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636502875806675749&sdata=QoVauX%2FoMyjwuq7qMJ6W73Oc9qGjWT8sHpj5thcBGB8%3D&reserved=0, or mute the threadhttps://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAfb9zuEQfaqrQfVXpZnZG6apLT-BrG0Dks5tFv7bgaJpZM4RPuU-&data=02%7C01%7C%7Ca845c9cf4a664edbed7f08d54ffe4299%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636502875806675749&sdata=paduCl6FVjttHURNxnDj2nl8vD2sNzdY58OnkcI3huU%3D&reserved=0.

PythonCHB commented 6 years ago

Thanks!