Start Coding with Python
… some practical exercises to get you started.
Why Learn Python?
Python is an essential programming language to learn if you’re looking to transition into a technical role. Python is used globally for numerous applications, from building robots to web development, and of course, data science and natural language processing.
Here are three reasons why you should learn Python:
It’s easy to read and learn. Often described as being syntactically similar to English, Python is known for its learnability.
Out-of-the-box libraries. There is a vast array of libraries — reusable bundles of code — to facilitate certain tasks. Need to manipulate data? Check out Pandas. Doing some math? Python has NumPy. Visualizing data? Try Matplotlib. Processing language? Explore NLTK and SpaCy. Building robots? Python can help you do that, too (I’ve never done it).
High demand. The demand for Python skills is ubiquitous across job postings in the data world, often alongside other key languages like SQL and R.
When I graduated with my BA in Spanish, I didn’t even truly know what ‘data science’ or ‘NLP’ was, let alone how to use Python in those contexts. Let me provide some examples of the power of Python.
Here’s how I have utilized Python to save time in my academic work as a linguist:
Data Extraction and Organization: I developed Python scripts to extract phonetic annotations from Praat TextGrids and organize this data into CSV files. This made it possible to conduct statistical analyses on the extracted data. It saved me HOURS.
Web Scraping for Corpus Building: I wrote a script to collect comments and tweets in multiple languages, such as Spanish, Italian, and Portuguese. This helped create corpora for my syntax and semantics research projects.
Lexical Resource Compilation: By writing scripts to parse through digital dictionaries, I extracted a list of 100 Turkish nouns that contained certain graphemes, which I used for the next bullet point.
Automated Presentation Creation: I utilized Python to automate the creation of PowerPoint presentations, each containing 100 slides with partially masked words designed for a phonetic production task. It is ridiculous how much time this saved me.
If you aren’t that familiar with data science or NLP, hopefully, this gives you a taste of how Python can be used in ways that may already be familiar to you.
If you are familiar with this, don’t worry, in the future we’ll dive into a wide variety of topics like data manipulation, data visualization, and even training machine learning models using Python!
So, let’s dive right in!
Coding Corner: Variables, Data Types, Functions
➡️ Click Here for a Bonus Tutorial & Follow Along in Google Colab ⬅️
Go to File → Save a Copy in Drive, navigate to that copy, and get started!
This tutorial isn’t exhaustive. It can’t be! Learning Python is a long journey, I hope this tutorial will help you embark on that journey.
So, what is a variable?
Think of a variable as a storage container for data or information.
So, just as the word ‘apple’ points to that physical fruit object that just appeared in your head right now, a variable holds values that tell the computer what to process. We can decide what the variable is and what it represents. It can represent numbers, text, or even other types of data. We can overwrite and change variables at any time!
Why should I care about variables?
Variables allow for flexibility in code because they allow you to store information, perform some operation on that information, and change it when necessary.
What can I store in a variable?
Here are some Python data types:
Integers: Whole numbers without a decimal point, like 3 or 100.
Floats: Numbers that include a decimal point, like 3.14 or 2.75.
Strings: Sequences of characters, often used to represent text, like ‘Hello, World!’.
Booleans: Values that are either True or False.
Lists: An ordered, mutable, heterogeneous collection of items such as integers, strings, floats, and even other lists!
Dictionaries: Dictionaries map one piece of data to another. Given some key, they return a value. You can ‘look up’ a value in a dictionary using a key, in the same way you can look up a definition in a dictionary given a word.
What is a function?
A function in Python is a reusable block of code that performs a specific task. You can pass data into a function, and it can return data as a result.
Think of a function as saying, “Hey, if you give me X, Y, or Z, I’ll perform some operation on it, and then give you something back.”
We use def to define a function, followed by the function name with parentheses directly after it. Inside the parentheses, you specify the data (arguments) needed by the function to perform its operations.
Here is a basic function that doubles any number you give it.
Below the definition of that function, we print out the result that we would get if use it to double the number 4.
As you can see, we have def to define the function, followed by the name of the function double_number, along with parentheses and the argument number within those parentheses.
Why are we passing in a number? Think of it this way: if I ask you to double any number I give you, and I want you to double a specific number, I need to provide you with that number to double!
Simple enough, right? Functions can certainly get a bit more complex than that. Let’s take a look at a contextualized example.
Here is a function that calculates the average spending of customers.
Of course, some libraries can help us do these operations, but the goal is to see it all spelled out!
Don’t worry if you don’t understand everything you see just yet, take some time to think about what each part might accomplish.
The function calculate_average_spend calculates the average spend of customers. To do that, it needs customer information, which is passed in as an argument.
As you can see, the function first gets the monthly spend for each customer using customer['monthly_spend'] for customer in customers, then sums it using sum(). Then, we find out how many customers there are in total using len(customers). Finally, we divide the total spend by the number of customers with average_spend = total_spend / number_of_customers, and return that as the answer.
Why should I care about functions?
Functions allow you to break down complex tasks into smaller, manageable, reusable pieces of code. Imagine needing to write the code shown above every time you need to calculate the average spend for every set of customers you’re analyzing! That’s a lot of code. All you’d have to do is write it once, then use the function where necessary.
What are conditional statements?
Conditional statements, often called "if statements," allow you to execute certain pieces of code only when specific conditions are met. Here is an example of how we might categorize data using if-statements inside functions.
The logic behind the logic
If X condition is satisfied (if), then do Y operation. Otherwise, if some other condition is satisfied (elif), then do a different operation. If none of those work (else), then do this other thing.
Python has if, elif, and else. Elif only runs if the first if is false (e.g. if we had 75 as spend). If the elif here evaluates false, then we return “Low spender.”
Note: We can write comments to annotate our code in Python using the # symbol. This proves to be extremely useful when you revisit your code and need to remember what you wrote.
Let’s Apply It! Follow along on Google Colab
Check it out. There are bonus explanations there!
Anyway, remember customer churn from last week’s article?
If you missed last week’s article, check it out here.
Customer churn is when a business loses a customer. As you can imagine, this is something businesses want to avoid! We can use data science to help us understand and counteract customer churn.
Here is a sample dataset of customers, their names, subscription status, monthly spending, and their comments.
In the code snippet below, we calculate the churn rate of our customers and determine the sentiment of their comments using a keyword approach.
If we run this snippet of code, we should get an output that looks like this:
Customer Churn Rate: 33.33%
Customer Mike's comment sentiment: Neutral
Customer Carol's comment sentiment: Neutral
Customer Dave's comment sentiment: Positive
Customer Eva's comment sentiment: Neutral
Customer Frank's comment sentiment: Neutral
Customer Grace's comment sentiment: NegativePretty cool, right? Now, it’s your turn to try!
It’s Your Turn!
This is what moves the needle! Take some time to work through the exercises below.
Wait!
Before you do that, do this.
Write this on a note: “Every challenge is an opportunity to grow”
Every time you’re feeling stuck, or every time Python yells at you for writing something incorrectly, look at that note.
Quick Tip: Don’t know what type of data is stored in a certain variable? Maybe you’re just starting out, or you forgot what you stored in that variable. It happens to the best of us. Use type(variable_goes_here) to see what type of data is stored in your variable.
Exercise: Mastering the Fundamentals
Declaring Variables: For each customer, manually add a key-value pair to the dictionary. For example, this could be customer age or some other data.
Data Types: Identify all of the data types within the
customerslist. What is each element in the list? What data types are the keys and values within those elements?For Loops: Write a loop to print out each customer’s name from the
customerslist.Mathematical Operators: Write code to print out customer data for customers with even ID numbers.
Basic Python Data Structures and For Loops: Write code to find how many customers are active and how many are inactive, then print these counts.
For Loops and Conditional Statements: Print the comments left by customers who are not currently subscribed (inactive).
Mathematical Operations and Averages: Calculate the average monthly spend of all customers and print it.
Exercise: Digging a Little Deeper
Expand the Sentiment Analysis:
Our current approach for sentiment analysis isn’t ideal. For example, Mike’s comment Everything is fantastic! was labeled as 'Neutral.’ Grace’s comment This place never leaves me disappointed ! was labeled as ‘Negative’.
Diagnose the issues that underlie these errors. How might we change our
determine_sentiment()function to account for cases like these?How could you refine this function to handle mixed sentiments or neutral comments that don't contain any keywords?
Detailed Monthly Spend Analysis:
Calculate and print the average monthly spend for customers with positive, negative, and neutral sentiments separately. What does this tell you about the spending behavior relative to their sentiments? How reliable is this measurement, given the aforementioned issues with our sentiment approach?
Customer Feedback Themes:
Beyond simple positive or negative classification, consider developing a simple method to identify common themes or words in negative feedback. What might be common concerns or praises from customers?
Thanks for Reading!
Thanks for joining me on this issue of Coding Corner here at Dive Into Data!
I hope this helps you get one step closer to achieving your goal of entering a technical field from a non-technical background.
Stay curious and keep exploring—there’s so much more to learn and achieve with Python. Be patient, and take it one day at a time.
If you found this helpful, I’d love to hear from you. Please respond to this email or comment on this post to let me know your thoughts!
Don’t forget to subscribe for more insights and updates. Until next time, happy coding!
Resource Roundup: Best Python Tutorials for Absolute Beginners
Eager to learn more Python? I’ve hand-picked these resources just for you! Each resource is a bit different, so you can learn the way you like. These are completely FREE.
I Want to Hear from You!
Help me shape the future of Dive Into Data by taking two minutes to fill out the reader survey. It covers your interests, preferences, and educational background so I can deliver content that resonates with you and supports your learning journey.







