Python Basics

Ghaida Bouchaala
The Startup
Published in
4 min readSep 15, 2020

Being a documentarian of what I do, is my new way of learning since reading “Show your work”. It highlights the importance of sharing our work and revealing our creativity…

And since I have recently joined a new internship whose main objective is to be able to apply machine learning algorithms to build predictive models… I started learning Python

By going through this article you’ll be able to learn the essential concepts of Python programming such as data types, tuples, lists, dictionaries, functions, regular expressions, Error & Exceptions, scope, Object-Oriented Programming…

Benefits of programming with Python :

  1. Python is widely used.
  2. Python is so easy to work with and read.
  3. Can be used in almost all fields.
  4. Python is a dynamic programming language.

What makes Python a dynamic programming language?

  • Variables are not bound to types.
  • It states the kind of variable in the runtime of the program.

First Step :

Before we start diving, you can just install Python and begin working with the Python Shell installed.
But I would recommend installing Anaconda which is an open-source distribution of the Python and R programming languages.
It brings many of the tools used in data science and machine learning with just one install.

After installing Anaconda, you can open the Jupyter Notebook to start coding.

Ps: You can consider coding in the In-browser IDE repl.it by creating your python Repl.

Now it’s time we start learning and coding :

  • The famous “Hello World”:

So we use the print() function to print a specified message to the screen, and as you can notice, Python does not require semi-colons to terminate statements.

  • Numbers:
    Numbers in Python have two main forms:
    1. Integers: exp 12
    2. Floating-point: exp 12.5
  • Variable assignment:
    An assignment sets a value to a variable. In order to assign a variable a value, we use the equals sign (=)
  • Strings:
    1. Are used in Python to hold text information and are indicated with the use of single quotes ‘ ‘ or double quotes “ ”.
    2. Are a sequence of characters, implying they can be indexed using bracket notation [].
    3. Strings are immutable
  • Lists:
    Lists are Python’s form of Arrays.
  • Dictionary:
    An unordered, changeable and indexed collection with key-value pairs.
  • Tuple:
    An ordered and immutable collection of objects that cannot be changed, unlike lists, and use parentheses.
  • Set:
    An unordered collection of unique elements.

When to use List, Dictionary or Set?

* Use a list when you care about the order because a list keeps order, dict and set don't.

* A dict uses the key-value association, while list and set just contains values.

* A set expects items to be hashable, a list doesn't. So for non-hashable items, you must instead use list and cannot use set.

*A set bans duplicates, while it is allowed within a list.

  • If .. else statements:
    Python maintains the usual logical conditions of mathematics ( ==, !=, >, ≥ , < , ≤)

Note that to indicate a block of code, Python uses indentation.

  • Loops:
    1.while loop executes a set of statements as long as a condition is true
    2.for loop executes a set of statements, once for each item in a list, tuple...

It is also possible to use the break, continue, and else statements with while and for loops, each having its own role when used.

  • List Comprehension:
    A neat way to define and create a list in Python
  • Functions:
    A function in Python is defined using the def keyword
    Here’s the general syntax for creating and calling a function
  • Lambda:
    A Lambda function is an anonymous function meaning that it has no name. It usually has no more than a line, can take any number of arguments, but can only have one expression.
  • Scope:
    Python Scope follows the LEGB Rule:
    - Local: names assigned in a function ‘def or lambda’, and not declared global in that function
    - Enclosing Function locals: names in the local scope of all enclosing functions (def or lambda), from inside to outside.
    - Global: names assigned to the top level of a module file or declared global in a def function within the file.
    - Built-in: names preassigned in the built-in names module
Python’s scope hierarchy
  • Object-Oriented-programming:
    Well, this subject is very essential and fundamental to deepen your knowledge of python. I’ll cover the syntactic part of creating a class and instantiating it.
  • Error & Exceptions:
    Two types of error occur in python:
    1- Syntax errors for which the program will stop the execution.
    2- Exceptions that are raised when certain internal events occur and modify the normal flow of the program.
  • Regular Expressions:
    A regular expression is a special sequence of characters that serve to match or find other strings or sets of strings, using a specialized syntax included in a pattern.

The article is finally coming to an end. Hope this covers what you should know, fellow reader, as a newbie to python programming.

I can now say that learning Python is by far a good decision I made, to advance my programming skills. I hope this applies to you too.

And keep in mind that the idea here is not to memorize everything at once, but just be able to use this article as a reference.

You can start applying what you learned by practising here. Enjoy it!

--

--