DEV Community

Anne Wariara
Anne Wariara

Posted on

PYTHON 101

The best way to learn a programming language is to do as much practice as you can. Well, maybe you are wondering how do I go about it, it is simple. You can install python onto your machine or you can still practice by using many of the online interpreter such as; https://www.python.org/shell/ or https://replit.com/languages/python3 just to mention a few. With that out of the way let us dive into the python basic shall we?

The Basics

------Table of contents------
Basic Python Introduction
Variables
Operators
Data Type
Comments
Enter fullscreen mode Exit fullscreen mode

Basic Python Introduction

In any programming language there is a set of rules that one has to follow so that we can tell a computer what to do. This set of rules are called syntax. It is super important to learn and use the correct syntax when writing code else will fall into an error called syntax error and the code will not execute at all.
Also to note, aside from the syntax, we have semantics which is basically the overall effect that the code has. It is possible to to have code that is in the correct format but does not do what we want it to do and this is an example of a semantic error.

Variables

Think of variables as containers that we use when we ask a computer to perform an operation for us. Variables come in handy when we need to store values for later reference. When we create a variable, the computer reserves a portion of its own memory to store the value, allowing the computer to read or edit the value later when need be.
Let us have a look at this simple script that calculate the sum of two numbers.
Image description
From the above code snippet, we assign the first number variable to five and the second number variable to ten. The process of storing a value inside a variable is called assignment. We assign the sum of the numbers with the result of the expression of first number added to second number. This is known as an expression.

Operators

Operators perform operation on variables. For instance we can use * operator to multiply two numbers.
In python, the commonly used operators are:

  1. Arithmetic operator: They are used to perform common mathematical operation and they are;+ => Addition - => Subtraction * => Multiplication / => Division ** => Exponentiation // => Floor Division % => Modulus
  2. Assignment Operators: They are used to assign values to variables. They include; = example is x = 10 += example x+=10 is the same as x = x+10 -= example x-=10 is the same as x = x-10 *= example x*=10 is the same as x = x*10 /= example x/=10 is the same as x = x/10 %= example x %= 10 is the same as x = x%10 etc.
  3. Comparison operators are used to compare two values. They include;
    == which is Equal
    != It means not equal to
    > means greater than
    < means less than
    >= Greater than or equal to
    <= Less than or equal to.`
    The following code snippet shows the above comparison operators.
    Image description

  4. Logical Operators are used during combination of conditional statements.
    Logical operators in python include;

  5. and which returns true if both condition is true

  6. or return True if one of the statements is true.

  7. not negates or gives a reverse of the result that is if True it returns False and vice versa.
    Below is a code snippet to illustrate the concept discussed.
    Image description

Comments

Comment are statements in Python that cannot be executed. Comments are crucial while writing code for they help us and others understand the code we write. In python single line comments are written starting with a hash character followed by the text we want commented.
Image description
For multiline comments we use triple quotation(''').
Image description

Data Types

In programming, data types play such a crucial part. Variables tend to store data in different types where the different types tend to do different things.
Just for reference when you want to know the what is the data type of a variable, we use a type() function which returns the object type.
In Python, the most commonly used data types are:

  1. strings: This is written text between quotes eg "Hello World!".

  2. Integer: This are whole numbers without a fraction i.e. 120, 1, 10.

3.Float: This represents real numbers or numbers with a fraction bit in them eg 2.8, 10.5 etc.

4.Boolean:This types of data type may have one of the two values; True or False
The code snippet illustrates the data types discussed above.
Image description
For more additional materials, please feel free to look at the following;
Python Documentation https://docs.python.org/3/
W3school Python https://www.w3schools.com/python/default.asp

Oldest comments (0)