Week 1: Learning to code in Public - Python
A productive week on my journey of learning code. I decided to commit to learning Python as my first programming language, having dabbled with Ruby on Rails and
I feel like it is the language with the most upside for my long term goals.
Why Python as your first language?
From my research, it was Pythons user-friendliness over other languages that appealed to me along with the following possibilities including;
Scripting - to automate boring and repetitive tasks.
Web Development - accompanied by a web framework like Django.
Data Science - including machine learning, data analysis, and data visualization
How are you learning Python?
My next challenge was deciding ‘’How'' I was going to learn Python, with such a vast array of options including Online Courses, Bootcamps, Books, etc. I decided to go back to basics and adopt a ‘medium’ (lol) of learning that I know and love - books! ''Automate The Boring Stuff with Python'' by Al Sweigart stood out to me, backed up with some stellar reviews on Amazon and Reddit, I was sold.
Even though my long-term goal is entrepreneurship there are repetitive tasks in my current job that I know that by writing some simple scripts with Python I can fully automate. This book, therefore, appealed to me as a way of capturing some quick wins while applying what I learn. I’ll be summarising my learnings for 1 chapter per week.
Chapter 1 Recap - Python Basics - Automate The boring Stuff With Python
If you’re new to programming, like me you're probably still wondering what is programming?
Simply put programming is the act of entering instructions for the computer to perform. For example; I.e Do this, then that, if this is true, do this, if not keep doing that until it is true. It’s like wiring a recipe for the computer to follow to produce a specific result instead of a tasty meal.
''A computer is like a Swiss Army knife that you can configure for countless tasks. Many people spend hours clicking and typing to perform repetitive tasks, unaware that the machine they’re using could do their job in seconds if they gave it the right instructions. Programming is simply the act of entering instructions for the computer to perform.’'
The opening paragraph reaffirmed I was in the right place and as to ‘’why'' I was beginning this journey in the first place. It also redressed to me a concern I’m sure a multitude of people experiences when learning or contemplating learning to code - do I need to be good at Math? To which Sweigart explains;
''Actually, most programming doesn’t require math beyond basic arithmetic. Being good at programming isn’t that different from being good at solving Sudoku puzzles. Just because Sudoku involves numbers doesn’t mean you have to be good at math to figure out the solution. Writing programs involves breaking down a problem into individual, detailed steps.''
This was music to my ears as quite frankly I’ve never been good at math but do enjoy solving problems. The book is broken into two sections the first covering the fundamentals of Python and the second covering how to as the name suggests a multitude of ways to Automate The Boring Stuff.
''For most people, their computer is just an appliance instead of a tool. But by learning how to program, you’ll gain access to one of the most powerful tools of the modern world, and you’ll have fun along the way.''
Expressions
An expression is an instruction that combines values and operators and always evaluates down to a single value. For example, this is an expression: >>> 2 + 2. The 2s are integer values and the + is the mathematical operator. This expression evaluates down to the single integer value 4
Operators
Operators are special symbols in Python that carry out arithmetic or logical computation. These rules for putting operators and values together to form expressions are a fundamental part of Python as a programming language, just like the grammar rules that help us communicate.
The Integer, Floating Points, and String Data Types
Integer; is a whole number, positive or negative, without decimals, of unlimited length.
Floating Point: represent real numbers and are written with a decimal point
String; A string in Python is a sequence of characters. It is a derived data type.
Variables
Variables are containers for storing data values. In Python, a variable is created the moment you first assign a value to it. A well-named variable clearly describes what it is storing. E.g if you’re moving house you would clearly label what box contains what - think of variables the same way.
You can name a variable anything as long as it obeys the following three rules:
1. It can be only one word.
2. It can use only letters, numbers, and the underscore (_) character.
3. It can’t begin with a number.
The print() Function; The print() function displays the string value inside the parentheses on the screen. The line print('Hello world!') means “Print out the text in the string
'Hello world!’.”
The input() FunctioN; The input() function waits for the user to type some text on the keyboard and press enter.
Printing the User’s Name; The following call to print() contains the expression 'It is good to meet you, ' + myName between the parentheses.
The len() Function; You can pass the len() function a string value (or a variable containing a string), and the function evaluates to the integer value of the number of
characters in that string.
The str(), int(), and float() Functions: The str(), int(), and float() functions will evaluate to the string, integer, and floating-point forms of the value you pass, respectively. If you want to concatenate an integer such as 29 with a string to pass to print(), you’ll need to get the value '29', which is the string form of 29. The str() function can be passed an integer value and will evaluate to a string value version of it, as follows:
My First Program
I first wrote my first 'Hello World' python program combining all of what I learned above.
After clicking save and fn + f5 - I ran my first Python program!
Conclusion
Expressions, and their component values— operators, variables, and function calls—are the basic building blocks that make programs. Once I know how to handle these elements I'll be able to instruct Python to operate on large amounts of data.
The print() and input() functions handle simple text output (to the screen) and input (from the keyboard). The len() function takes a string and evaluates to an int of the number of characters in the string. The str(), int(), and float() functions will evaluate to the string, integer, or floating-point number form of the value they are passed.
In the next chapter, I’ll learn how to tell Python to make intelligent decisions about what code to run, what code to skip, and what code to repeat based on the values it has. This is known as flow control, and it allows me to write programs that make intelligent decisions.
Please note that the above is my streamlined recap and if you really want to learn Python I’d highly recommend you pick up a copy of Al’s Sweigart book; Automate The Boring Stuff With Python.
You can Subscribe to my weekly updates on this journey at https://thebootstrappedbuilder.substack.com/