<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ana Richie on Medium]]></title>
        <description><![CDATA[Stories by Ana Richie on Medium]]></description>
        <link>https://medium.com/@anarichie?source=rss-61b463b85d16------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*90xL0ZyTEkv64ujfxkSYLw.jpeg</url>
            <title>Stories by Ana Richie on Medium</title>
            <link>https://medium.com/@anarichie?source=rss-61b463b85d16------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 03:37:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@anarichie/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Python: Getting Started with Pandas II]]></title>
            <link>https://medium.com/@anarichie/python-getting-started-with-pandas-ii-4269dd3091c5?source=rss-61b463b85d16------2</link>
            <guid isPermaLink="false">https://medium.com/p/4269dd3091c5</guid>
            <category><![CDATA[learning-to-code]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[learning]]></category>
            <dc:creator><![CDATA[Ana Richie]]></dc:creator>
            <pubDate>Mon, 18 May 2020 23:04:53 GMT</pubDate>
            <atom:updated>2020-05-18T23:04:53.242Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Modifying DataFrames</strong></p><p>one way we can add a new column to a DataFrame is by giving a list of the same length as the existing DataFrame. For example:</p><pre>df[&#39;New Column Name&#39;] = [4, 6, 8, 10, 2, 4, 12]</pre><p>We can also add a n ew column that is holds the same value in all rows:</p><pre>df[&#39;Available Immediately&#39;] = True</pre><p>Finally, we can add a new column by preforming a function on the other existing columns :</p><pre>df[&#39;Sales Tax&#39;] = df.Price*0.075</pre><p>we can use the <strong>apply</strong> function to apply function to every value in a particular column. For example, if we wanted to make all the the names in a list of names capitalised:</p><pre>df[&#39;Name&#39;] = df.Name.apply(upper)</pre><p>We can use (lower) for all lowercase letters.</p><p><strong>Reviewing Lambda</strong></p><p>Lambda functions work like regular functions but in a more concise form.</p><pre>lambda1 = lambda x: ((x*2)+3)</pre><pre>print(lambda1(5))<br>&gt;&gt;&gt; 13</pre><p>The general way of writing a lambda <strong>if </strong>function is as follows:</p><pre>lambda x: OUTCOME IF TRUE if CONDITIONAL else OUTCOME IF FALSE</pre><p>So how can we <strong>apply a lambda function to a column</strong> in Pandas?</p><p>For example, if we wanted to get the e-mail provider from a list of e-mails, we could use the .split() method.</p><pre>df[&#39;New Column Name&#39;]= df.Email.apply(lambda x: x.split(&#39;@&#39;)[-1])</pre><p>So from a list of e-mails for example, <strong>ana@hotmail.com</strong>, the result would be <strong>hotmail.com</strong>.</p><p>The string function will split the string into a list of strings at each space.</p><pre>print(&#39;This is a string&#39;.split())</pre><pre>&gt;&gt;&gt; [&#39;This&#39;, &#39;is&#39;, &#39;a&#39;, &#39;string&#39;]</pre><p>Now let’s look at how we can <strong>apply a lambda function to a row.</strong></p><p>If we use apply<strong> </strong>without specifying a single column and add the argument <strong>“axis = 1”, </strong>the input to our lambda function will be an entire row, for this we use the following syntax:</p><pre>row.column_name</pre><pre>or</pre><pre>row[&#39;column_name&#39;]</pre><p>Let’s get a closer look at an example. If we have a DataFrame with 3 columns, Item, Price and Is Taxed? and we would like to add a new column with shows the final price of the items in the DataFrame after tax, assuming the item is taxed, we can do it using the following code:</p><pre>df[&#39;Price With Tax&#39;] = df.apply(lambda. row:<br> row[&#39;Price&#39;] * 1.075<br> if row [&#39;Is Taxed&#39;] == &#39;yes&#39;<br> else row [&#39;Price&#39;],<br> axis = 1)</pre><p><strong>Renaming Columns</strong></p><p>We can change the name of all the columns in our DataFrame at once by using<strong> .columns</strong>.</p><pre>df = pd.DataFrame({<br> &#39;name&#39; : [&#39;Josh&#39;, &#39;Debby&#39;],<br> &#39;age&#39; : [28, 26]})</pre><pre>df.columns = [&#39;First Name&#39;, &#39; Age&#39;]</pre><p>Using this command, the existing DataFrame is edited.</p><p>You can also rename individual columns by using <strong>.rename</strong>.</p><p>First let’s take a look at the overall syntax to use .rename</p><pre>{&#39;Original Column1&#39; : &#39;New Name1&#39;,<br>&#39;Original Column2&#39; : &#39;New Name2&#39;}</pre><p>So let’s try to re-do our first example using .rename.</p><pre>df.rename(columns = {<br> &#39;name&#39; : &#39;First Name&#39;, <br> &#39;age&#39; : &#39;age&#39; },<br> inplace = True)</pre><p>We have to use inplace = True, to make sure the current DataFrame is updated rather than creating a new one.</p><p>If you misspell one of the original column names, the command won’t fail, but it simply won’t change anything.</p><p><strong>Aggregates in Pandas</strong></p><p>An aggregate statistic is a way of creating a single number that describes a group of numbers.</p><p>To combine all of the values from a column for a single calculation we can use the following:</p><pre>print(customers.age)<br>&gt;&gt;&gt; [23, 25, 35, 35, 46, 62]</pre><pre>print(customers.age.median())<br>&gt;&gt;&gt; 35</pre><p>The general syntax for these calculations is:</p><pre>df.column_name.command()</pre><p><strong>List of Common Commands</strong></p><ul><li>mean: average of all values</li><li>std: standard deviation</li><li>median: median</li><li>max: max value in column</li><li>min: minimum value in a column</li><li>count: count number of variables in a column</li><li>nunique: number of unique values in a column</li><li>unique: List of unique values in a column</li></ul><p>The general syntax for calculating aggregate functions in combination with groupby:</p><pre>df.groupby(&#39;column1&#39;).column2.measurement()</pre><p>column 1: column to group by</p><p>column 2 : column for measurement</p><p>measurement: function we want to apply.</p><p>After using a <strong>groupby</strong> we often need to clean up our results, because when we use it a <strong>Series</strong> is created. If we want those indices as a column, we use reset_index(). This will transform our series into a DataFrame and move the indices into their own column.</p><pre>df.groupby(&#39;column1&#39;).column2.measurement().reset_index()</pre><p>If the operation we want to preform is more complicated than <strong>mean</strong> or <strong>count</strong>, we can use <strong>apply</strong> and lambda functions, just like we did for individual column operations. The input for our lambda function will always be a list of values.</p><pre>high_earners = <br>df.groupby(&#39;category&#39;).wage.apply(lambda x: np.percentile(x,75)).reset_index())</pre><p><strong>Pivot Tables</strong></p><p>If we had a table that looked like:</p><pre>Location        Day        Sales<br>Manchester      M          300<br>Manchester      Tu         310<br>Manchester      W          320<br>...</pre><p>but think it would be more useful to view the able differently, as seen below:</p><pre>Location        M     Tu     W<br>Manchester     300   310    320<br>West Village   400   430    378</pre><p>We can do this by creating a pivot table. In pandas the command for pivot is:</p><pre>df.pivot(columns = &#39;ColumToPivot&#39;,<br> Index = &#39;Column To Be Rows&#39;,<br> Values = &#39;Column To Be Values&#39;)</pre><p>Like we learned about <strong>groupby</strong>, the output of a pivot command is a new DataFrame, so we need to use <strong>.reset_index()</strong> to prevent our indexing from being weird.</p><p><strong>Working with Multiple Tables in Pandas</strong></p><p>Inner Merge</p><p>The <strong>.merge</strong> method looks for columns that are common between two DataFrames and then rows where those column’s values are the same. It combines the matching rows into a single row in a new table.</p><pre>new_df = pd.merge(df1, df2)</pre><p>In addition using pd.merge, each DataFrame has it’s own merge method used as below:</p><pre>new_df = df1.merge(df2)</pre><p>We use this when we are joining more than two DataFrames together because we can “cain” the commands:</p><pre>new_df = df1.merge(df2).merge(df3).merge(df4)</pre><p><strong>Merge on Specific Columns</strong></p><p>In the previous example the function “knew” how to combine tables based on the columns that were the same between two tables. If this is not the case with the tables we are working with, one way we could address this is using <strong>.rename</strong> to rename the columns for our merges.</p><pre>pd.merge(df1, df2.rename(columns{&#39;id&#39; :&#39;df2_id&#39;}))</pre><p>If we don’t want to use this method, we can also use the keywords <strong>left_on</strong> or <strong>right_on. </strong>The ‘left’ table is the one that comes first and the ‘right’ table is the one that comes second.</p><pre>pd.merge(df1, df2, left on = &#39;df2_id&#39;, right_on = &#39;id&#39;, suffixes = [_df1&#39;, &#39;_df2&#39;] ) </pre><p>We use <strong>suffixes </strong>so that the merge doesn’t automatically rename the columns with _x and _y.</p><p>This is helpful when you merge two DataFrames together that have a column name that is the same name (for example, “id”) but the info does not correspond (for example, user id vs product id).</p><p>If there are any values which do not match using the pervious method, they will be left out when merging the DataFrames.</p><p>Inner Merge is the type of merge where two DataFrames whose rows don’t match perfectly, will result in the loss of the unmatched rows.</p><p>There are other types of merges that we can use when we want to keep information from the unmatched rows. <strong>An Outer Join would include all rows from both tables, even if they don’t match</strong>. Any missing values will be filled with ‘None’ or ‘nan’.</p><pre>pd.merge(df1, df2, how = &#39;outer&#39;)</pre><p>Left or Right Merge</p><p>A left merge includes all the rows from the first (left) table, but only rows from the second (right) table that match the first.</p><pre>pd.merge(company_a, company_b, how = &#39;left&#39;)</pre><p>In the code above, we would get all customers from company_a and only customers from company_b that are also customers of company_a.</p><p>Right merge is the exact opposite of left merge. Here the merged table will include all include all rows from the second (right) table but only rows from the first(left) that match the second.</p><pre>pd.merge(company_a, company_b, how = &#39;right&#39;)</pre><p><strong>Concatenate DataFrame</strong></p><p>A DataFrame might be broken into multiple tables. For example if we have multiple CSV files for the same set of information. When we need to reconstruct a single DataFrame from multiple ones, we can use <strong>concat</strong>:</p><pre>pd.concat([df1, df2, df3)]</pre><p>This method only works if all the columns are the same in all the DataFrames.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zvG4ksik0YKL1CQz" /><figcaption>Photo by <a href="https://unsplash.com/@priscilladupreez?utm_source=medium&amp;utm_medium=referral">Priscilla Du Preez</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4269dd3091c5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Python: Getting Started with Pandas]]></title>
            <link>https://medium.com/@anarichie/python-getting-started-with-pandas-da684ddeb2af?source=rss-61b463b85d16------2</link>
            <guid isPermaLink="false">https://medium.com/p/da684ddeb2af</guid>
            <category><![CDATA[learning-to-code]]></category>
            <category><![CDATA[learning]]></category>
            <category><![CDATA[notes]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Ana Richie]]></dc:creator>
            <pubDate>Sun, 17 May 2020 12:16:00 GMT</pubDate>
            <atom:updated>2020-05-17T12:16:00.928Z</atom:updated>
            <content:encoded><![CDATA[<p><em>I’m learning Python, and as a revision method I’ve decided to type up all my notes. So just keep that in mind if you’re here looking for answers, I’m not an expert!</em></p><p>Pandas is a python module for working with tabular data.</p><p>The module is typically imported at the top of a python file, as <strong>pd.</strong></p><pre>import pandas as pd</pre><p>If we import the module as described above, whenever we need to access it, we can simply operate on <strong>pd.</strong></p><p><strong>Creating DataFrames</strong></p><p>A DataFrame is an object that stores data as rows and columns. It can be compared to a spreadsheet, or a SQL tables for example. Each column has a name made up of a string, and each row has an index which is an integer.</p><p>You can create a DataFrame from a dictionary. The columns must all be the same length, otherwise you will get an error.</p><pre>df = pd.DataFrame( {<br> &#39;name&#39; : [&#39;Ana&#39;, &#39;Bobby&#39;, &#39;Justin&#39;]<br> &#39;address&#39;: [&#39;123 Apple Street&#39; , &#39;456 Orange Court&#39;, &#39; 789 Cinnamon Road&#39;]<br> &#39;age&#39; : [24, 28, 31]<br> })</pre><p>This code would create the following table. Notice that the columns will automatically be arranged into alphabetical order.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/818/1*MQPE_OIsEkHhWVt_wLg1SA.png" /></figure><p>You can also add data to a DataFrame by passing on a List. Take a list of lists for example, each list represents a row of data. We can do this in combination with the keyword argument <strong>columns</strong>, which allows us to pass a list of column names.</p><pre>df2 = pd.DataFrame([<br> [&#39;Ana&#39;, &#39;24&#39;, &#39;123 Apple Street&#39;],<br> [&#39;Bobby&#39;, &#39;28&#39;, &#39;456 Orange Court&#39;],<br> [&#39;Justin&#39;, &#39;31&#39;, &#39;789 Cinnamon Road]<br> ], columns = [&#39;name&#39;, &#39; address&#39;, &#39;age&#39;])</pre><p>Using this method, we are able to control the order of the columns.</p><p>The most common format to pass data into a DataFrame is using CSV files (a text-only spread-sheet format). In a CSV file, the top row will contain column headings, and all subsequent rows will have values, everything being separated by a comma.</p><p>When you have data in a CSV file, we can load it into a DataFrame using <strong>.read_CSV()</strong></p><pre>pd.read_csv(&#39;CSV_filename&#39;)</pre><p>If we want to see/display a DataFrame, we can simply print it. However, this only efficient when working with a small set of data. When it comes to working with large DataFrames, it’s best to inspect a few items, rather than loading the entire data set. We can do this using<strong> .head()</strong>. This will by default give the first five rows of a DataFrame. If we’d like to display more rows, we can do this by positioning an argument.</p><pre>df.head(10)</pre><p>If you want to get some general information on a DataFrame, you can use <strong>df.info().</strong></p><p><strong>Selecting Columns</strong></p><p>There are two possible ways to select all values from a column.</p><ol><li>Select the column as if we were selecting a value from a dictionary using a key.</li></ol><pre>dataframe_name[&#39;Column Name&#39;]</pre><p>2. If the name of a column follows all of the rules for a variable (doesn’t start with a number, doesn’t contain spaces or special characters) then you can select it using a different method:</p><pre>dataframe_name.column</pre><p><strong>Selecting Multiple Columns</strong></p><p>To select two or more columns from a DataFrame, we can use a list of column names.</p><pre>new_df = dataframe_name[[&#39;Column1&#39;, &#39;Column2&#39;]]</pre><p>Make sure to use a double set of brackets [[]] or the command will not work.</p><p><strong>Select Rows</strong></p><p>DataFrames are “zero-indexed”, this means that they being on the 0th row and count up from there.</p><p>To select a single row, we can use the following syntax:</p><pre>.iloc[index]</pre><p>So, if we wanted to select the third row of DataFrame called orders we could do so by using the following code:</p><pre>orders.iloc[2]</pre><p>Remember DataFrames are “zero-indexed” so we need to call index number 2 to get the 3rd row.</p><p>The result of using <strong>.iloc[]</strong> will be a ‘series’.</p><p><strong>Select Multiple Rows</strong></p><p>There are a few different ways to select multiple rows in combination with <strong>.iloc[]</strong></p><pre>df.iloc[3:8]<br>#This would select all rows starting from the 3rd and up to but not including the 8th row.</pre><pre>df.iloc[:4]<br>#This would select all rows up to but not including the 4th row</pre><pre>df.iloc[-3:]<br>#This would select all rows starting at the 3rd to last and up to and including the final row. </pre><p><strong>Selecting Rows with Logic</strong></p><p>You can select a subset of a DataFrame by using logical statements.</p><pre>df[df.column_name == desired_column_value]</pre><p>Let’s look at an example where we want to select all the students who are 24 years old from a DataFrame. We can use the following code to achieve this.</p><pre>df[df.age == 24]</pre><p>If we wanted to select all rows where the students age was under 20 or where the student was called ‘Joel’, we could use the following code</p><pre>df[(df.age &lt; 20 ) | (df.name == &#39;Joel&#39;) ]</pre><p>In Python, we use | as OR and &amp; as AND.</p><p>If we wanted select all the rows where the students names are either; ‘Joel’ , ‘Benji’ or ‘Billie’ we could use the <strong>isin</strong> command.</p><pre>df[df.name.isin([‘Joel’ , ‘Benji’, ‘Billie’])]</pre><p><strong>Setting Indices</strong></p><p>When we select a subset of a DataFrame using Logic, we end up with a non consecutive indices.This is not very ideal, so we can fix it by using <strong>.reset_index(). </strong>However, using <strong>.reset_index() </strong>will create a <strong>new</strong> column, and the old indices will not have been removed. To remove them, we must use <strong>drop = True</strong></p><pre>df.reset_index(drop= True)</pre><p>Using <strong>.reset_index()</strong> will return a new DataFrame. If we want to modify our existing DataFrame, we can use the keyword <strong>inplace= True</strong></p><pre>df.reset_index(inplace= True, drop = True)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*G8LhfpZwkIwAD4Cv" /><figcaption>Photo by <a href="https://unsplash.com/@goran_ivos?utm_source=medium&amp;utm_medium=referral">Goran Ivos</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=da684ddeb2af" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Python: Getting Started with Loops]]></title>
            <link>https://medium.com/@anarichie/python-getting-started-with-loops-e7ff73a174a9?source=rss-61b463b85d16------2</link>
            <guid isPermaLink="false">https://medium.com/p/e7ff73a174a9</guid>
            <category><![CDATA[notes]]></category>
            <category><![CDATA[learning]]></category>
            <category><![CDATA[learning-to-code]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Ana Richie]]></dc:creator>
            <pubDate>Sun, 17 May 2020 11:16:00 GMT</pubDate>
            <atom:updated>2020-05-17T11:16:00.856Z</atom:updated>
            <content:encoded><![CDATA[<p><em>I’m learning Python, and as a revision method I’ve decided to type up all my notes. So just keep that in mind if you’re here looking for answers, I’m not an expert!</em></p><p><strong>Creating A Loop</strong></p><p>A loop allows one to preform an action on each item in a <a href="https://medium.com/@anarichie/python-getting-started-with-lists-54c611bbdcc1">list</a>.</p><p>The general way of writing a loop:</p><pre>for TEMPORARY VARIABLE in LIST VARIABLE:<br> ACTION</pre><p>The temporary variable can be anything! The two examples seen below would work exactly the same.</p><pre>for i in song_titles</pre><pre>for song in song_titles</pre><p><strong>Using Range</strong></p><p>If we want to preform an action multiple times, we need a list with a <strong>length</strong> of the number of times we want to this repeated. We can do this using <strong>range</strong>.</p><pre>for i in range(3):<br> print(&quot;Hello&quot;)</pre><pre>&gt;&gt;&gt; Hello Hello Hello</pre><p>Loops can accidentally be set to be infinite. Be careful not to do this or you will need to restart your program.</p><p><strong>Break and Continue</strong></p><p>If we want a loop to stop at a certain point, for example, if we were searching for a value and wish for it to stop once found, we can use <strong>break</strong> inside the loop. When a program hits a <strong>break, </strong>control will return to the code outside the loop.</p><pre>Numbers = [1, 2, 3, 4, 5, 6]</pre><pre>for num in Numbers:<br> if num == 4:<br> break <br> print(num)</pre><pre>&gt;&gt;&gt; 1,2,3</pre><p>While we iterate through (go-through) a list, we also may want to skip certain values. This can be done by using <strong>continue</strong>.</p><pre>Numbers = [2, -3, -1, -5, 1, -2, 3, -4]</pre><pre>for num in Numbers:<br> if num &lt; 0 <br> continue<br> print(num)</pre><pre>&gt;&gt;&gt; 1,2,3 </pre><p><strong>While Loops</strong></p><p>A while loop will preform a set of code, until a certain condition has been reached. This can be used to iterate through lists just like for loops. Every-time the condition inside the while loop is satisfied, the cone inside will run. This is useful when you don’t know how many iterations are needed to reach the wanted point.</p><pre>Singles = [ &quot;Holding on to You&quot;, &quot;Guns for Hands&quot;, &quot;Lovely&quot;, &quot;House of Gold&quot;, &quot;Car Radio&quot;] </pre><pre>index = 0</pre><pre>while index &lt; len(Singles):<br> print(Singles[index])<br> index += 1</pre><pre>&gt;&gt;&gt; Holding on to You<br>Guns for Hands<br>Lovely<br>House of Gold<br>Car Radio</pre><p>As you can see, we set the index equal to 0, and every time the loop runs, we add 1 to the index. We’ve set the loop to run, while the index number is smaller than the <strong>length</strong> of the list “Singles”. This is useful when we don’t know the length.</p><p><strong>Nested Loops</strong></p><p>Nested Loops can be used to go through a list of lists.</p><pre>Cities = [[&quot;The Hague&quot;, &quot;Amsterdam&quot;, &quot;Rotterdam&quot;], [&quot;Tampico&quot;, &quot;Puebla&quot;, &quot;Villahermosa&quot;], [&quot;Sofia&quot;, &quot;Burgas&quot;, &quot;Bansko&quot;]]</pre><pre># Here we have 3 lists within one list. Each list contains city names from different countries.</pre><pre>for country in Cities:<br> for x in country:<br>  print(x)</pre><pre>&gt;&gt;&gt;The Hague<br>Amsterdam<br>Rotterdam<br>Tampico<br>Puebla<br>Villahermosa<br>Sofia<br>Burgas<br>Bansko</pre><p>Here we use <strong>two temporary</strong> variables. For <strong>country in Cities</strong> refers to the first set of lists within the list. For <strong>x in country</strong>, refers to the items within the smaller lists.</p><p><strong>List Comprehensions</strong></p><p>If we want to create a new list with a pre existing list, we know we can do this with a loop. For example</p><pre>Words = [ &#39;@Ana&#39;, &#39;#Python&#39;, @Bobby, &#39;#Lists&#39;, &#39;#Values&#39;, &#39;@Maria&#39;]</pre><pre>for word in words:<br> if word[0] == &#39;@&#39; <br>#if the first index of the item in the list is equal to @<br> usernames.append(word)<br></pre><p>The code above takes a list of @s and #’s and sorts through them to create a new list, called usernames, where only those items in the list that started with @ are taken.</p><p>Another way we can create this list is as seen below:</p><pre>Words = [ &#39;@Ana&#39;, &#39;#Python&#39;, &#39;@Bobby&#39;, &#39;#Lists&#39;, &#39;#Values&#39;, &#39;@Maria&#39;]</pre><pre>usernames = [word for word in words if word[0] == &#39;@&#39;]</pre><p>With this method we can also do math when the lists are made up of numbers.</p><pre>Numbers = [1, 2, 3, 4, 5]</pre><pre>numbers_plus = [n + 100 for n in Numbers]</pre><pre>print(numbers_plus)<br>&gt;&gt;&gt; [101, 102, 103, 104, 105]</pre><p>The general structure for a list comprehension is</p><pre>New_List_Name = [OPERATION for ITEM in LIST]</pre><p><strong>Lambda Functions</strong></p><p>“A lambda function is a small anonymous function.” A basic lambda function could looks like this:</p><pre>add_two = lambda input: input + 2 </pre><pre>print(add_two(3)) <br>&gt;&gt;&gt; 5</pre><p>Here’s another example, using a lambda function to search for an item within a string:</p><pre>is_substring = lambda string: string in &quot;Hello, how are you?&quot;</pre><pre>print(is_substring(&#39;I&#39;)) <br>&gt;&gt;&gt; False</pre><pre>print(is_substring(&#39;H&#39;))<br>&gt;&gt;&gt; True</pre><p>We can also use <strong>if </strong>statements with lambda functions.</p><pre>WHAT TO RETURN IF TRUE if IF STATEMENT else WHAT TO RETURN IF FALSE</pre><p>Below we have an example:</p><pre>even_or_odd = lambda num: &quot;even&quot; if num % 2 == 0 else &quot;odd&quot;</pre><pre>print(even_or_odd(1))<br>&gt;&gt;&gt; odd</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*IM6oG48nkX7RpCjr" /><figcaption>Photo by <a href="https://unsplash.com/@fatosi?utm_source=medium&amp;utm_medium=referral">Fatos Bytyqi</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e7ff73a174a9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Python: Getting Started with Lists]]></title>
            <link>https://medium.com/@anarichie/python-getting-started-with-lists-54c611bbdcc1?source=rss-61b463b85d16------2</link>
            <guid isPermaLink="false">https://medium.com/p/54c611bbdcc1</guid>
            <category><![CDATA[learning-to-code]]></category>
            <category><![CDATA[notes]]></category>
            <category><![CDATA[learning]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Ana Richie]]></dc:creator>
            <pubDate>Thu, 14 May 2020 21:38:42 GMT</pubDate>
            <atom:updated>2020-05-14T21:38:42.581Z</atom:updated>
            <content:encoded><![CDATA[<p><em>I’m learning Python, and as a revision method I’ve decided to type up all my notes. So just keep that in mind if you’re here looking for answers, I’m not an expert!</em></p><p><strong>The Basics</strong></p><p>To create a list, use <strong>[ ] </strong>and use commas to separate items</p><pre>List_Example = [&#39;Ana&#39;, &#39;Bobby&#39;, &#39;Tyler&#39;, &#39;Jenna&#39;, &#39;Josh&#39;, &#39;Debby&#39;]</pre><p>It is also possible to create an empty list</p><pre>Empty_List = [ ]</pre><p>You can add one value to a list, using <strong>.append()</strong></p><pre>List_Num = [1, 2, 3]</pre><pre>List_Num.append(4)</pre><p>You can combine multiple lists by creating a new one and using <strong>+</strong></p><pre>List_Num = [1, 2, 3]</pre><pre>List_Num2 = List_Num + [4, 5, 6]</pre><p>Another way you can combine two lists, is using <strong>zip()</strong></p><pre>New_List = zip(old_list1, old_list2)</pre><p>You can also create lists that follow a ranged pattern:</p><pre>Up_To_Six = range(7)</pre><pre>print(Up_To_Six)<br>&gt;&gt;&gt; 0, 1, 2, 3, 4, 5, 6</pre><p>The range will stop 1 number before the stated one. (So basically the count will go up to that number) Notice it starts at 0.</p><p>You can also start your range on a different number and have it follow a pattern.</p><pre>Skip_2 = range(2,9,2)</pre><pre>print(Skip_2)<br>&gt;&gt;&gt; 2, 4, 6, 8</pre><p>This list skips every two numbers, starting at 2 and ending at 9</p><p><strong>Working with Lists</strong></p><p>We can find the length of a list by using <strong>len</strong></p><pre>Names = [&#39;Ana&#39;, &#39;Bobby&#39;, &#39;Tyler&#39;, &#39;Jenna&#39;, &#39;Josh&#39;, &#39;Debby&#39;]</pre><pre>print(len(Names))<br>&gt;&gt;&gt; 6</pre><p>Python lists are “zero-indexed” — this means the first element in a list has an index of 0. We use index numbers to get information from a list. For example, to find the first or last element in a list:</p><pre>Names = [&#39;Ana&#39;, &#39;Bobby&#39;, &#39;Tyler&#39;, &#39;Jenna&#39;, &#39;Josh&#39;, &#39;Debby&#39;]</pre><pre>print(Names(1))<br>&gt;&gt;&gt; &#39;Ana&#39;</pre><pre>print(Names(-1))<br>&gt;&gt;&gt; &#39;Debby&#39;</pre><p>Lists can be also be sliced, we can do this by using the following code</p><pre>List_Name [Start:End]</pre><p>Start = Index of the first element to be included</p><p>End = Index of <strong>a number higher </strong>than the last index we want to include (so if we want to include up to 8, we input 9 and so on)</p><pre>Names = [&#39;Ana&#39;, &#39;Bobby&#39;, &#39;Tyler&#39;, &#39;Jenna&#39;, &#39;Josh&#39;, &#39;Debby&#39;]</pre><pre>Numbers = [ 1, 2, 3, 4, 5, 6]</pre><pre>print(Names[2:4])<br>&gt;&gt;&gt; Tyler, Jenna, Josh</pre><pre>print(Numbers[1:3]<br>&gt;&gt;&gt; 2,3 </pre><p>If you wanted to, for example, select the first 3 elements, you can also do this by using [0:3] or [:3]. We could also select the last few items by using [5:0] or [5:0]. If we wanted to select the <strong>last</strong> numbers, we can use negative numbers [-3:] — this would select the last 3 items of the list.</p><pre>Names = [&#39;Ana&#39;, &#39;Bobby&#39;, &#39;Tyler&#39;, &#39;Jenna&#39;, &#39;Josh&#39;, &#39;Debby&#39;]</pre><pre>Numbers = [ 1, 2, 3, 4, 5, 6]</pre><pre>print(Names[:4]) #Start from 0 and end at 3<br>&gt;&gt;&gt; Ana, Bobby, Tyler</pre><pre>print(Names[4:]) #Start at 4 and end at the end of the list<br>&gt;&gt;&gt; Josh, Debby</pre><pre>print(Numbers[-3:] #Selects last 3 items in the list<br>&gt;&gt;&gt; 4, 5, 6</pre><p>We can use the function <strong>.count()</strong> to count within a list</p><pre>Votes = [ &#39;Tom&#39;, &#39;Tom&#39;, &#39;Matt&#39;, &#39;Mark&#39;, &#39;Travis&#39;, &#39;Tom&#39;,&#39;Mark&#39;, &#39;Travis&#39;, &#39;Travis&#39;, &#39;Tom&#39;,&#39;Mark&#39;,&#39;Matt&#39;]</pre><pre>Tom_Votes =  Votes.count(&#39;Tom&#39;)<br>print(Tom_Votes)<br>&gt;&gt;&gt; 4</pre><pre>Travis_Votes = Votes.count(&#39;Travis&#39;)<br>print(Travis_Votes)<br>&gt;&gt;&gt; 3</pre><p><strong>Sorting Lists</strong></p><p>There are different ways we can sort a list. The first method is to sort it in place, we do this by using <strong>.sort(). </strong>This will sort the list ascending by default.</p><p>When assigned to a variable, .sort() will not return anything, but it will sort the selected list.</p><pre>Games = [ &#39;The Sims&#39; , &#39;PacMan&#39;, &#39;Pokemon&#39;, &#39;Tetris&#39;]</pre><pre>Games.sort() </pre><pre>print(Games)<br>&gt;&gt;&gt;[&#39;PacMan&#39;, &#39;Pokemon&#39;, &#39;Tetris&#39;, &#39;The Sims&#39;]</pre><p>After sorting the list we need to call to print() to view the result.</p><p>To sort “out of place” or — in a new list, we can use <strong>sorted</strong></p><pre>Games = [ &#39;The Sims&#39; , &#39;PacMan&#39;, &#39;Pokemon&#39;, &#39;Tetris&#39;]</pre><pre>print(games_sorted)<br>&gt;&gt;&gt;[&#39;PacMan&#39;, &#39;Pokemon&#39;, &#39;Tetris&#39;, &#39;The Sims&#39;]</pre><p>Notice that .<strong>sort()</strong> goes after the list name, while <strong>sorted </strong>goes before.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*BP57nJ4tQ6RxBFCQ" /><figcaption>Photo by <a href="https://unsplash.com/@jefflssantos?utm_source=medium&amp;utm_medium=referral">Jefferson Santos</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=54c611bbdcc1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Python: Introduction to Functions]]></title>
            <link>https://medium.com/@anarichie/python-introduction-to-functions-1d3dbb9cc244?source=rss-61b463b85d16------2</link>
            <guid isPermaLink="false">https://medium.com/p/1d3dbb9cc244</guid>
            <category><![CDATA[learning]]></category>
            <category><![CDATA[learning-to-code]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[notes]]></category>
            <dc:creator><![CDATA[Ana Richie]]></dc:creator>
            <pubDate>Thu, 14 May 2020 20:45:40 GMT</pubDate>
            <atom:updated>2020-05-14T20:45:40.962Z</atom:updated>
            <content:encoded><![CDATA[<p><em>I’m learning Python, and as a revision method I’ve decided to type up all my notes. So just keep that in mind if you’re here looking for answers, I’m not an expert!</em></p><p><strong>Functions</strong></p><p>Some tasks need to be performed multiple times within a program. Rather than re-write the code in multiple places, a function may be defined using <strong>def. </strong>It’s important to use <strong>(): </strong>when defining a function.</p><pre>def the_judge():<br> print(&quot;When the leader of the bad guys sang something soft and soaked in pain, I heard the echo from his secret hideaway&quot;)</pre><p>Python uses indentation to identify blocks of code. Code within the same block should be indented at the same level.</p><p><strong>Calling Functions</strong></p><p>A function can be called by writing the name of it, follow by ()</p><pre>the_judge()<br>&gt;&gt;&gt; When the leader of the bad guys sang something soft and soaked in pain, I heard the echo from his secret hideaway</pre><p><strong>Parameters</strong></p><p>Sometimes functions need input to provide data for their code. This is defined using parameters. They are defined by including this information inside the brackets.</p><pre>def add_one_mult_five(number): <br> print(number + 1 * 5)</pre><pre>add_one_mult_five(1)</pre><pre>&gt;&gt;&gt; 10</pre><p>Functions can have multiple parameters. To define a function with multiple parameters, the names must be placed one after another, separated by commas in the parentheses where the function was originally defined.</p><p>Parameters are placeholders for actual values that function needs. When the function is called, these values are passed as arguments.</p><pre>def add_y_mult_x_(number, y, x) #These are the parameters<br> print(number + y * x)</pre><pre>add_y_mult_x(2,8,1) #These are the arguments<br>&gt;&gt;&gt; 10</pre><p><strong>Returns</strong></p><p>The keyword return is used to return a value so it can be modified or used later. When there is a result from a function that can be stored in a variable, it is called a returned function value.</p><pre>def div_by_two(input_n)<br> return input_n/2</pre><pre>#The program can then call div_by_2 and use the result later</pre><pre>Result = div_by_two(6)</pre><pre>print(Result)<br>&gt;&gt;&gt; 2<br></pre><p>You can also return text (strings)</p><p>You can also return multiple values by separating them with a comma.</p><pre>def many_values(x,y,z)<br> x_plusone = x + 1<br> y_plusone = y + 1<br> x_plusone = x + 1<br> return x_plusone, y_plusone, x_plusone</pre><p>Parameters cannot be referenced from outside the scope of the function but global variables can be accessed inside functions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*haqIgwa9pKvkCN6E" /><figcaption>Photo by <a href="https://unsplash.com/@goumbik?utm_source=medium&amp;utm_medium=referral">Lukas Blazek</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1d3dbb9cc244" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to Python]]></title>
            <link>https://medium.com/@anarichie/introduction-to-python-132eb24d4158?source=rss-61b463b85d16------2</link>
            <guid isPermaLink="false">https://medium.com/p/132eb24d4158</guid>
            <category><![CDATA[learning-to-code]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[notes]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Ana Richie]]></dc:creator>
            <pubDate>Thu, 14 May 2020 20:18:30 GMT</pubDate>
            <atom:updated>2020-05-14T20:18:30.177Z</atom:updated>
            <content:encoded><![CDATA[<p><em>I’m learning Python, and as a revision method I’ve decided to type up all my notes. So just keep that in mind if you’re here looking for answers, I’m not an expert!</em></p><p><strong>Comments</strong></p><p>Comments in Python are written using a #.<br>They are used to write text which the code will ignore when it runs. This can be useful to describe what your code is doing below or to make notes for yourself to look at later.</p><pre>#Hello, This is a comment :)</pre><p><strong>Print</strong></p><p><strong>print()</strong> is used to “tell a computer to talk”</p><pre>print(&quot;Hello&quot;)<br>&gt;&gt;&gt; Hello</pre><p><strong>Strings</strong></p><p>Strings are “blocks of text”. Must be surrounded by double or single quotes (<strong>“”</strong>) or (<strong>‘’</strong>) — it can be either one but you should try to be consistent.</p><pre>&quot;This is a String&quot; <br>&#39;This is also a String&#39;</pre><p><strong>Variables</strong></p><p>A variable is a method of storing data for reuse. Assign a variable using =</p><pre>Variable1 = 1 + 2</pre><p><strong>Numbers</strong></p><p>Python has a few numeric data types.</p><ul><li>An integer <strong>int</strong> is a whole number</li><li>A floating-point number <strong>float</strong>, is a decimal number</li></ul><pre>int = 6<br>float = 3.2</pre><p><strong>Calculations</strong></p><p>Python can preform simple calculations such as addition (+), subtraction (-), multiplication (*) and division. Keep in mind that when performing a division, python will convert all ints to floats before.</p><p>Variables that are assigned numeric values can be treated the same as numbers themselves.</p><pre>Variable1 = 5</pre><pre>Variable2 = 3 + Variable</pre><pre>print(Variable2)<br>&gt;&gt;&gt; 8</pre><p>Furthermore, Python can also perform exponentiation using <strong>** </strong>and a companion to the division operator called the modulo operator, indicated by <strong>%</strong>, it gives the remainder of a division calculation</p><pre>#Variable1 represents 2 squared</pre><pre>Variable1 = 2**2</pre><pre>print(Variable1)<br>&gt;&gt;&gt; 4</pre><pre>#Variable2 is using the % Modulo operator. This will provide the remainder of a division calculation.</pre><pre>Variable2 = 4%2</pre><pre>print(Varaible2)<br>&gt;&gt;&gt; 0</pre><p><strong>Concatenation</strong></p><p>The <strong>+ </strong>operator doesn’t just add two numbers, it can also “add” two strings. It does not add a space in between, so if you’re building a sentence, you’ll need to add the spaces in yourself.</p><pre>T1 = &quot;Hey!&quot;<br>T2 = &quot;How are you?</pre><pre>print( T1 + T2)<br>&gt;&gt;&gt; Hey!How are you?</pre><pre>print( T1 + &quot; &quot; + T2)<br>&gt;&gt;&gt; Hey! How are you?</pre><p><strong>Plus Equals</strong></p><p>When you have a number saved in a variable and want to add to the current value of the variable, you can use <strong>+=</strong></p><pre>Miles_Walked = 3</pre><pre>Miles_Walked += 2</pre><pre>print(Miles_Walked)<br>&gt;&gt;&gt; 5</pre><p><strong>Multi-Line Strings</strong></p><p>If a string occupies multiple lines (a paragraph) and/or contains a lot of quotation marks, we use <strong>“””</strong> to open and close the string to make sure python understands clearly what is in the string.</p><pre>Circles_Lyrics = &quot;&quot;&quot; We couldn&#39;t turn around<br>&#39;Til we were upside down<br>I&#39;ll be the bad guy now<br>But no, I ain&#39;t too proud<br>I couldn&#39;t be there<br>Even when I try<br>You don&#39;t believe it<br>We do this every time &quot;&quot;&quot;</pre><pre># Through the use of &quot;&quot;&quot; we avoid the code confusing the line breaks and &#39; symbols as part of the code</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0LcAXW5BDqmzGEOS" /><figcaption>Photo by <a href="https://unsplash.com/@chuklanov?utm_source=medium&amp;utm_medium=referral">Avel Chuklanov</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=132eb24d4158" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>