Python Basics in under Fifteen Minutes

Lakshmi Prakash
Design and Development
7 min readMar 29, 2022

Python is God because Python is omnipotent, omniscient, and omnipresent. 🤪 You will never regret learning to code in Python. I was quite happy when I learnt to program in Python, for I don’t come from a programming background. Also, python is one of the most loved and sought after skills in the programming field today. But I also heard a data scientist say, “any idiot can program! Programming is nothing. It’s the real skills that employers look for!” Ha ha, then I didn’t know how to feel about this new skill I’d learnt. Also, I’ll let you know when I hear back from this person on what they mean by “real skills”. Nonetheless, nothing changes the fact that programming in Python is both fun and powerful, so I figured I’d write on the basics of Python for anyone new to Python, for those coming from a programming background and for those who don’t.

Trust me, Python is very simple and easy to start with!

Python uses indentation for a new block of code: While in some languages, you would use indentation merely to make it easier for developers or readers to read through the code, in Python, the point of indentation is more than making it easier for your eyes and understanding. It’s a way of demarcating a new block of code. Indentation must be carefully used in Python. For example, everything that happens within a for loop must strictly have the same indentation, 4 white spaces further from the “for” line to show that all those lines need to be executed when the for loop would run.

Python uses # to ignore lines of code, for us to leave comments: Comments play an important role in helping programmers communicate with one another on what we’re intending to do with a certain line or block of code. The way to leave “comments in Python is by starting a line of code with # or the hash symbol. You can also comment out multiple lines by using a # in the beginning of each of them.

Python code to show commenting and indentation

Python is an Object-Oriented Programming Language: What does that mean? Initially, I found this a bit hard to understand. It means that, in Python, everything is an “object”, a piece of data, or “code”, procedures called “methods”. And object-oriented programming languages allow you to create “abstractions” of code, which can be modified to create new instances of the same abstraction. For example, let’s consider creating a class for “NewBook”? What are the elements or aspects that define books in general? Think about it? All books would have one or more authors, a publisher or publishing company, one or more editors, one or more languages in the content, a certain number of pages, a genre (self-help or science fiction or statistics or finance or politics, something like that), cost, and such. Now, you could create an e-book or a hard copy with the same elements included because these apply to all books.

Commonly used Data Types in Python: Guess what would happen with the “sum” function if I give two input values as “five” and “six” instead of 5 and 6? You’re not going to get the answer you expect. This is where data types come in and you need to know what are all the different data types used in Python. Whole numbers are considered “int” referring to the data type integer and decimal numbers are of the data type “float” referring to “floating point numbers”. Words like names or sentences or phrases come under the data type “str”, referring to “string”. Booleans are used to state whether a condition is “True” or “False” (yes, the first letter is capitalized).

You can also change the data types from one to another, which again is easily doable in Python: defining and changing types of objects as we go.

Data Structures in Python: “Lists”, as the name suggests are lists of items. For example, the list of students in a class would be a “list”. Dictionaries, that are denoted by “dict” in Python programming, can be considered a list of “key”: “value” pairs. For example, let’s say you want to store the gender of students along with their names. In that case, you’d not require a list but a dictionary. And a “tuple” is a collection of values in which, once defined, the order of the collection or the collection itself can’t be modified. It’s called “immutable”.

Conditional Statements and Loops: The “if” and “while” code works in Python the way it does in other programming languages. If you are new to programming, sometimes, you might find it tricky to decide when to use “if” and when to use “while”, but don’t sweat it in the beginning. With practice, you’ll learn your way around. Note that sometimes looping can be infinite, running with no end, and it’s through mistakes like this that we’d learn the art of coding.

Conditions and Loops in Python

What are “args” and “kwargs”? In Python, you define a class or function by using the word “def”. If you are going to create a function that takes in two numbers as input, sums them up, and returns the output, you could define the function by saying something like this.

def sum(x, y):
return(x + y)

result = sum(5, 6)
print(result)

Now, as per the definition, the function “sum” takes two values as inputs. If you give anything less or more than two inputs, you’d receive an error because Python doesn’t know what to do with it. And you need to give the inputs in the same order according to the definition. Since this function returns the sum, it doesn’t matter, (a + b) = (b + a). But if it were subtraction or division, for example, you would want to be careful with the order.
To make matters easier and keep things more accurate, you can use “kwargs” aka “keyword arguments”. You can define arguments by using “keywords” while defining a function and also define the types of data that are to be given as input. This way, the user knows clearly what the arguments are.

def coffee_consumption(name = str, type = str, number = int):
return(f"{name} has at least {number} cups of {type} a day.")

johns_details = coffee_consumption(name= "John", type= "black coffee", number= 3)
edwards_details = coffee_consumption(name="Edward", number=2,type= "mocha")
asifas_details = coffee_consumption(type= "iced latte", number=1, name= "Asifa")
print(f"{johns_details} \n{edwards_details} \n{asifas_details}")

Math Operators in Python: Here is a list of math operators you can use to perform simple mathematical operations —

For the more complicated mathematical operations, you will need to use Python libraries.

Packages and Libraries You’ll have to Learn: If you are interested in machine learning, you should familiarize yourself with Python libraries such as Python, NumPy, Scikit Learn, and PyTorch. Pandas would be one of the most commonly used packages in Data Analysis. Beautiful Soup is an interesting library frequently used in web scraping. Depending on your interests and goals, you can choose from a wide range of packages and libraries you’d enjoy working with in Python.

Now, why learn Python? Python is one of the most commonly used languages in automation, data analysis, and data visualization. If you’re into data science, you’d benefit a great deal by learning to code in Python. Also, it’s relatively very simple to learn, even for beginners or those who are new to programming. Even if programming is only a hobby or new interest, chances are high that you’d still enjoy working with Python.

General Tips for Python:

  • Remember that casing is important in Python. What does this mean? “Apple” and “apple” are not the same in Python; these are identified as two different objects, so make sure that you get the casing right.
  • Indentation is how Python recognizes which lines of code fall within a loop and where a loop ends. If you check out the if/while loops, you can see that the lines of code that are supposed to be executed within the loop would clearly be indented. Without indentation, Python won’t understand how to differentiate between the different lines of code.
  • One good news for junior Python developers and starters is that the Python community is very active around the world and is one of the most preferred languages today, so for almost any problem that you encounter, you can find solutions shared by someone on websites such as stackoverflow.com.
  • Try practicing as much as you can. Check out the documentation for different libraries, and try to execute some of those functions yourself. That is the best way to learn. No amount of theoretical knowledge will suffice. This applies to not just Python but to all coding languages.

--

--

Lakshmi Prakash
Design and Development

A conversation designer and writer interested in technology, mental health, gender equality, behavioral sciences, and more.