12
Feb

Ep. 5: Yandex Practicum Lesson 2: Python Strings, Lists, and Loops


everyone and welcome to this next
episode of computers Python and data science this is gonna be especially
awesome episode just cuz it’s my birthday today so let’s get it huh I
think this is gonna be over lists if I remember what they said last time
correctly and it looks like the theme is going to be emojis so let’s get started
next more emoji stuff I guess okay here we go
strings let’s see where’s the description make this a little bit
bigger here you guys recognize these emojis these ones at the bottom I don’t
know I’ve never used those before okay but it looks like fractional numbers are
classified as float data we learned that last time
however texts and symbols in Python are classified as the STR or string datatype
which stands for string we’ve already dealt with strings before but did not
yet explain that they have a different datatype so basically anything you put
in quotes like they have here tears of joy that’s gonna be a string datatype
okay and then they just have a comment I guess explaining the type in Python a
string is a sequence of any symbols in either double or single quotation marks single quotation marks can’t be utilized
when you have to place quotation marks within strings the heart emoji is one of
the most popular so you see they inter-lay they have on the outside
they have single quotes and on the inside they can use double quotes
because they don’t match up together and if you’re already in a string you can’t
make another string so let’s create a string with the following text let’s see
where’d that go we’re studying the statistics of various emojis all right
so they have the intro text here I don’t like that they write these comments and
for us but that’s okay we’re studying Oh looks like we should probably use double
quotes because we’re gonna have to use a apostrophe in here we’re studying the
statistics of various emojis also have to have a
period there and it’s ended with a double quote all right so let’s check actually didn’t even run it there’s
nothing printed so you seem to have made an error make sure your output meets the
required conditions then stored in the variable intro text and print it on
the screen using print so we didn’t print it that’s why so then we’ll do
print intro underscore text it’s helped us to read all the instructions ok it
looks right right so I’ll check it correct awesome let’s do task number two strings can sometimes consist of more
than just letters they may include symbols try to print out strings with
the symbolic alternative of several emojis you can see them depicted in the
table below so once this new smiley face and laughing and kiss I guess so doesn’t
want us to print all three let’s see okay so what if I do : okay there’s one and we’ll print that
and then we’ll copy paste this a couple times I guess it’s kind of weird they
don’t exactly tell you what they want here maybe I’m missing something so we
do D and – who actually puts the dash in their emojis for the nose I’ve never
done that I always just had the face of me so I guess I was oh you had hit next now
now it’s actually showing okay so smile we’re gonna put here is now it makes
more sense okay and then laughing is equal to that right and then kiss here
it’s a : – star okay let’s see if that prints seems about
right let’s check it correct awesome so let’s see what the
next one is Emojis can also be written in two
strings however they may might not be displayed correctly in specific browsers
and operating systems for that reason we won’t use them in our code but in this
task you can try and see if they work for you the check button in this task
does not actually check anything click it in order to advance to the next task
all right so let’s just see if it works works in our browser how silly alright here we go now we’re
starting with list it’s what I thought we were gonna get into create a list
name Instagram containing the first five elements of the column Instagram comma
mil the elements must be a float type I guess we skipped the introduction of
this okay now that we know how to store single elements of tables values of
float and string datatypes let’s learn how to turn strings and columns into
tables in order to do that we need a new data type list it’s a sequence of values
similar to an array using other programming languages the list is the
first data structure you will learn data structures organize given elements and
are configured according to specific rules like for example sequential
storing in a list in order to create a new list in Python you must first list
all the elements separated by commas within square brackets for example
here’s a list with the first 5 values of emoji Express mil column the list is
stored in the variable emoji Express and can be printed on the screen using print
we can imagine that the variables are boxes that store values and that the
list is a shelf that the boxes are sitting on the Shelf has a name seeing
as the list itself will be stored in the variable and each box on the Shelf has
its own number these numbers are called indices please take note that the first
element in a list is assigned to the index oh oh here oh sorry in order to
obtain an element from the list and and do what you want with it you will have
to indicate its index within a pair of square brackets alright so that’s a
whole lot of information I think it’s gonna be easier just to do it to help
understand so they want us to do the first 5 elements of the column Instagram
comma mil which is here so we’ll do write the code here remember they said
let’s start to the bracket a square bracket and then inside the bracket
we’re gonna do the values the first one is zero point seven seven four then you
put a comma and then you put the next one seven four
three one then you put a comma then we scroll down to point three six let me
put a comma then four point two six now I have one two three four so we need one
more eleven point two all right so let’s run that and see if that’s what they’re
looking for check it incorrect value for variable Instagram first five elements of the column
Instagram mil oh I skipped a few sorry I wasn’t scroll up to the top I guess one
point zero two comma one point six nine zero point seven seven four okay and
then we take off things last two now and make sure that you don’t have that last
comma if you don’t have an element there okay so we hit play one point zero to
one point six nine okay let’s check that correct awesome so we go the next one you don’t have to maintain the same
value types a third list for example we can keep both strings and numbers in the
same list create a list of elements the tables first row and then print it out
to the screen skip the symbol column it will not be
required this time around so the first row is grinning and they said to put the
first let’s get rid of this comment I hate these I wish they just didn’t put
those in okay two point two six comma one point zero two comma 87 point three
comma and there’s nothing in property so do
they want us to put a blank string is out there looking for I guess we should pipe up grinning in
here as well huh that’s first grinning I look great too you guys let’s see awesome we did it next okay next
now let’s calculate the proportion of the selected emojis first figure out the
sum of the first five elements we’ve prepared within the emoji Express list
however don’t add the numbers themselves add the elements obtained by indexing
instead then print the result on the screen the format for the output has
already been provided for you in the pre-code okay so the total is going to
be emoji express and they said remember to access an element you put a bracket
and in the index of it which is going to be 0 and then we want to add emoji
Express 1 we’ll copy paste make a little bit easier ok so we have 0 plus 1 plus 2
Plus 3 how many they weren’t they weren’t 5 yeah first 5 elements so we
have 0 1 2 3 and we’d one more four alright so let’s see if that works yeah we’ll check it awesome we did it next automation of
list our previous example has demonstrated that we all need a better
tool for sorting through lists in the loops lesson you’ll learn how to repeat
the same actions for every element list in the addition assignment lesson to
learn how to add value of one to another and in the summation of loops you’ll
combine the tasks okay so inside the Twitter variable is a
list of values from us we should read loops first should we okay a loop is a
structure for repeating functions within code let’s begin with an easy exercise
print out the first help 10 elements of the table emoji expressed and they do
for element in emoji express so here’s the loop portion you shouldn’t have seen
before for element in emoji express so it’s like they’re doing they’re going
through this this list which is called emoji express right and it’s got 1 2 3 4
5 6 7 8 9 10 elements in it right and so it’s gonna do 4 for each element and of
the 10 elements so when students accessing say the first time it goes to
a loop element is going to be equal to 2 point 2 6 the next time it goes through
the loop it’s going to be 19 point 1 the next time it goes it’s gonna be 25 point
6 and so on let’s see if they explain that better here this loop orders for
every element of the list emoji Express do this and then calls the print
function for each element a loop structure or syntax looks similar to
this they give you a image of what we already saw loops are declared by using
the keywords for and in hence they’re referred to as a for loop while loop
smart while using loops margins in python will highlight the code applied
to each designated element loop body although margins are nothing more than a
bunch of spaces they’re vital in order to help Python differentiate the loop
body from the rest of the code let’s look at an example so you can see
they’re just showing you here look how there’s an indentation right so the
print is on the same indentation as the for loop start but then everything
that’s inside the loop is in the next indentation so this is indented this is
indented this isn’t it and it’s and so on okay in order to insert for space you
can hit the tab button once the interactive platform with an
automatically convert the tab relations symbol into spaces Python does not
comprehend code that uses both tabulation and spaces which is another
flaw of Python but it’s okay okay so inside the Twitter variable is a list
values from the column Twitter mill for the top ten emojis print a column with
these values in the following format Twitter mill they do a – and then they
want us to write a for loop that iterates through the Twitter we do for
element in Twitter : you press ENTER and it auto tab for me that’s nice
then it wants us to print the element right alright so I think that should be
all we need yep so you can see eighty six seven
point three and so on alright so let’s check that correct we
can play the task okay cool alright so task number two the total
number of emojis sent with the emoji Express keyboard is one point seven two
billion or seventeen twenty million for each of the top ten emojis calculate
their proportion of the total amount then present that share as a percentage
precise to the first decimal place in the following format okay so this kind
of combining what we did in the last lesson with the for loop here so let’s
see so against the total and then we do emoji share :
and then print the shares so what they want to use a loop here so we do for
element and what is it emoji Express emoji Express : divide the quantity of
the specific emoji by the total okay so we do element / / emoji Express total and yeah I should
keep with the consistent spacing right they said you should have spaces between
your things oh so this is gonna be part okay and
then these should each be on their own indentation level right okay so that’s
gonna print that mode you share the emoji share the emoji share and then the
total emoji so I think I think that’s correct right let’s run it okay so if we
scroll up emoji share point one percent one point one percent all right that
looks correct right so we’ll check it all right cool we completed the task
that’s next the addition assignment all right let’s
scroll down a little bit the next step is to summing up whole list the next
step to summing up whole list is learning how to add individual values
and storing them as variables please keep in mind when calculating the value
of one variable you can use other variables as well okay so they have
grinning beaming and raffle and then they print total okay later on the value
of this variable can be changed so then they do total equals total plus beaming
total plus raffle okay do you recall that an equal sign is the assignment
operator first the value to the right of it will be calculated and the result is
stored in the variable to the left okay oftentimes you will need to add a
value to a variable and at the same time store the sum within it in order to do
that there is a special addition assignment operator which is plus equals
with it the code total equals total plus beaming can be written as total plus
equals beaming similar abbreviations also exist for other arithmetic
operations so you have a equals a plus B is a plus equals B a minus equals B a
times equals B and a divide equals B okay so now we calculate the total
number of hands feature than these particular emojis all right so we need to do total let’s see so total hands originally zero
and they want us to add kiss which is it’s weird that they they have kissing
and they also have kiss mark they don’t just have single kiss right so that that
could be an issue you know all right we’ll just do the kiss for now
which is 21.7 it’s also confusing because this this comment is we will put
it after before I guess here we new 21.7 thumbs up is 23.1 raffle is 25.6 dang they wants to do a bunch total
hands plus equals winking is 15.2 thinking thinking is six point eight one I’m also confused cuz kiss doesn’t have
a thumb in it or doesn’t have a hand in it does it yeah I really don’t get the whole what
they’re doing the emojis here we’ll see though we’ll see so shrugging has 1.74
right total hands plus equals 1.7 forward
oh they give you much more supposed to use though right here so let’s see so
let’s be this one eighty seven point five and maybe it has a hand on a
different browser or something total hands is eighty seven point five
right now let’s run that see what we get 1:59 let’s see incorrect let’s check again okay so we
have the kissing one right and let me what do we say that was 87.5 yep that
looks right then they have the thumbs up picture down here which is where’d it go there it is twenty three point one okay
and we have waffle which is twenty five point six yeah that’s right
winking is fifteen point two thinking is six point eight one right let’s see yep
stinkiness six point eight one shrugging is one point seven four so what are we missing guys calculate the total number of hands
featured in these particular emojis well so maybe it’s like I said before
the kiss doesn’t actually have a hand in it so if we put zero here
the thumbs up does oh okay wait a minute I think I was I was putting the number
here and I think it really just wants the number of hands I feel it’s kind of
silly now okay so the total number of hands in the thumbs up is 1 the total
number of hands in the laughing is zero in the winking is 0 and the thinking
there’s one right and in the shrugging there’s two all right let’s see if
that’s right let’s check yeah we did it okay next okay task
number to calculate the sum of the first five emojis by using the addition
assignment operator in order do that’s sort of the transitional values and the
final result in total finally print the result rounded to two decimal place it’s
already been completed for you in the pre-code okay so equate the total to
zero so total equals zero add the value with index zero total plus equals emoji
Express at index zero okay add the value with the index one total plus equals
emoji Express one okay add the value with index two so you have total plus
equals emoji Express – okay and then we do total plus equals emoji Express thing
up here I misspelled it that’s yeah watch for that too I guess because
there’s three and then index for total plus o plus C equals emoji Express hold
and then it prints it for us so let’s see to 95.1 six it is correct
so next summation with loops let’s calculate let’s see if we there’s a
description for this one before we start automating the list summation there’s
one more step that has to be taken beining the use of addition assignment
and loose and the previous lesson we calculated the sum of the first few
elements of the column emoji express as so you can also add additional values by
using a loop rather than doing it manually for each count in Express count
I take the total and plus equals count I don’t know what this colon is doing here
oh I think this is actually supposed to be up here right after our for loop
you’re supposed to have a colon here for some reason it’s on the next line using
loops is convenient because now we can sum up lists of any given length so so
let’s calculate the per the proportion of you chosen emojis within the emoji
Express the total quantity of messages sent with the emoji Express keyboard is
one point seven two million billion or one or seventeen twenty million emojis
sum of the quantity of every emoji in the table and then divide by the sum
then present that share as the percentage accurate to the first decimal
place just like in the pre-code okay so selected total plus equals count the shares let’s see is this just supposed to be the selected
total selected total / the emoji total emoji
Express total underscored alright so what they’re
looking for 40.4% hey all right let’s go next task
to calculate that a portion of our chosen emojis on Twitter in total twenty
four point five billion or twenty four thousand five hundred million emojis
have been registered their name the variable used for saving some selected
total all right so the selected total is originally equal to zero and then we
need to iterate through the loop like they did in the last example for element
and Twitter selected total plus equals element and then we get that shared just
like we did last time by doing its the selected total divided by the Twitter
total okay and so that we get a float
originally I’m gonna put in I don’t think that matters Bob at that point
zero thirty nine percent nice we did and that’s the end of this
module guys so thanks so much for watching I hope to catch you next time

Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

There are no comments yet

Why not be the first

Leave a Reply

Your email address will not be published. Required fields are marked *