Getting started with Python : Syntax

Let’s talk about it!

So you’ve chosen the programming language you want to learn, what now?

Confused Will Smith GIF - Find & Share on GIPHY

Have you guys ever watched strands of spaghetti sink into a pot filled with hot water? Well that’s the perfect depiction of me trying to figure out where to start with the amount of Python courses, YouTube videos and articles I’ve come across; dramatic, I know.

I managed to narrow down the pool of resources using the steps below:

Step 1 : Know Your Level

One thing I knew for sure is that I needed to start at beginner level, in fact, I even second guessed whether beginner level would be a bit too ambitious for me, a complete amateur.

Step 2: Know how you learn best

I’m a visual learner and I learn by doing. I needed to find a course that will provide me with the knowledge, allow me to demonstrate what I have learned, and a quiz to test my knowledge.

Try to identify how you learn best, you may not figure it out in the beginning but during your journey you will come to realise what works best for you.

Step 3: Begin

The moment you have been preparing for, you’re ready, make a start!

SYNTAX

I’m going to give you a whistle stop tour of the syntax I learned when I first began the #100DaysofCode challenge!

I attended an opening day event that I found on google for people who were interested in coding, hardware and 3D printing.

I met a guy there probably in his 40s who is a programmer, we spoke for about an 1hr about programming including how much he loved Python due to its simple syntax. He strongly advised that I learned Python sytnax before diving into anything else, he really knew his shit so I follwed suit! 

Python syntax is ‘a set of rules which defines how a Python program will be written.’

COMMENTS

Writing comments are used to describe parts of a code; they are useful for those of us writing the code to make a note of what the code does so when we revist a section of code , it’s easy to remember, especially if it was written over a long period.

It’s also an easier way for other developers reading your code to figure out how it all works, especially if the lines of code are unclear. Comments dont need to be added to every line of code.

An interesteing but probably obvious fact that I learned is that computers do not run comments. (Go easy on me, I’m a noob).

PRINT

Next, I learned about the print() function, which tells the computer whatever message you want it to print out into the console.

Example:

The output of the example above would be:

STRINGS

A string is a block of text either surrounded by double quotes ” ” or single quotes ‘ ‘. It is perfectly up to you which one you choose. However, here’s the kicker; if your string contains single quotes within the text (I’m, It’s, Can’t) then surrounding the text with a single quote could generate an error.

Here’s an example of every newbies favourite string:

“”” MULTI-LINE STRINGS “””

Another fab thing about Python is that we can create a string that has multiple lines surounded by using three quote marks “””” or ”’.

VARIABLES

So variables took me a while to wrap my head around. I charge this to probably diving straight into exercises instead of probably watching a video that explains using variables in depth. But I’m happy that I’ve really gotten the hang of it now.

Here’s how I now understand variables; a named location used to store data which can be changed later.

age is the variable, I’ve used the = operator to assign the value 25 to the variable.

Here I’ve changed the value of a variable from 25 to 30.

The biggest lesson for me whilst doing exercises featuring varibles was that variables are case sensitive. age, Age, and AGE are three different variables. Attempting to use them interchangeable will generate a NameError. Mind fuck right!?

Here is a good link on variables: https://www.youtube.com/watch?v=Z1Yd7upQsXY [10:24]

ERRORS

So I’ll put my hands up and admit that I am a frequent flyer when it comes to making errors lol.

In this lesson I learned about SyntaxError and NameError.

SyntaxError – means there is something wrong with the way your program is written (via codecademy).

A SyntaxError has been the bane of my programming journey. Exhibit A:

Spot the SyntaxError = print(“This will generate a SyntaxError’)

NameError – This happens when the Python interpreter sees a word it does not recognise. Such as the example I mentioned above in Variables.

NUMBERS

Stay with me here, I won’t be going all Alan Turing on you.

It’s actually pretty simple. Python has two data types:

integer, int – a whole number

floating-point number, float – a decimal number.

Using variables, which I spoke about earlier, here’s an example:

I randomly chose the number 2 for this example, this is known as a literal as its an actual number and not a variable.

CALCULATIONS

Oh how I love how smart computers are, they’ll do the calculations that my Left brain can’t bring itself to do.

I learned that we can perform +-*, and / using Python.

Examples:


CHANGING NUMBERS

We can create variables and assign numbers to them and perform calculations using them.

Example:

Now I bet you’re wondering what burger costs £7.50? I take it you’ve never been to Five Guys!

EXPONENTS

Pretty sure we’ve all learned about exponents in math class. Forgive me if not!

It’s pretty simple, Python can perform exponentiation (didn’t know this was a word).

Example:

MODULO

As I type, I’m hoping that one of you reading this can further explain the modulo operator to me on twitter (@MillennialRach) or provide me with a good link.

I just don’t get it, I’ve tried, still no luck. brain is fried, #sendhelp

CONCATENATION

I really like the use of this Syntax. The + opeartor can be used to combine two strings which is called string concatenation.

Example:

greeting_text = “Hi mate!”

question_text = “How are you?”

full_text = greeting_text + ” ” + question_text

# Prints “Hi mate! How are you?”

print(full_text)

Note that I included ” ” in order to create a space between the texts.

PLUS EQUALS

This was the last part of Syntax I learned on the course.

The += operator can be used to update variables.

Example:

Useful course: https://www.codecademy.com/learn/learn-python-3

There you have it! I’ll continue to document my journey and you can also follow it on my twitter page @MillennialRach

Rach, Millenial Rach.

Social media & sharing icons powered by UltimatelySocial