Introduction to modern python

davidgitonga

David-Gitonga

Posted on April 22, 2022

Introduction to modern python

Python is an interpreted, general, high-level programming language invented by Guido van Rossum and conceived in 1991.
Python is a friendly, easy and simple language.

What makes python simple?

It's syntax emphasizes readability thus it is very simple to read, write and understand the code.

Applications of python

Web development
Data science
Machine learning and artificial intelligence
Game development
Desktop applications
Automation

Python installation

Visit https://www.python.org/downloads/[](url)
Select your Operating system
Select the release or version, click on it to download.
Double click on the file to execute and install. For window mark "add to system paths" An alternative way to install python on Linux is to run

> sudo apt install python3 or sudo apt install python on the terminal

Variables in python

A variable is a pointer to an object. Once an object is assigned to a variable you refer to the object by that name. But the data itself is contained in the object. In python you don't have to declare the python before using them or declaring their type.

Note

  1. Values stored in variables can be changed during program execution.
  2. A variable is only a name given to an object and all the operations done on the variable affect the object.

    Rules for creating variables.

    1. Must start with a letter or underscore character.
    2. Variable cannot start with a number.
    3. Can only contain alpha-numeric character and underscore(A- Z,0-9, _)
    4. Variables are case sensitive, (age, Age and AGE) are different variables
    5. Keywords cannot be used to name variables.
#declaring variables
age = 45
Enter fullscreen mode Exit fullscreen mode

Literals in python.

This is data given in variables.
Python has different types of variables;

  1. String literals
  2. Numeric literals
  3. Boolean literals
  4. Literal collections
  5. Special literal

1.String literals

These are characters that are surrounded by single or double quotes.

#In single quote
name = 'David'

#In double quotes
name = "David"
Enter fullscreen mode Exit fullscreen mode

2.Numeric literals

They are of different types;

  1. Integers
  2. Float
  3. Complex

1. Integers
These are both positive and negative integers without floating point numbers.

a = 2
Enter fullscreen mode Exit fullscreen mode

2. Float
These are both positive and negative numbers with
floating
point numbers(with decimals).

 num =0.2
Enter fullscreen mode Exit fullscreen mode

3. Complex
These are numbers having both a real and imaginary part in the form of x + 5j, where x is real and y is imaginary

3. Boolean literals

They are only two;
True
False

4. Literal collections

These are;
List
Tuples
Dictionaries
Set

5.Special literals

Python contains one special literal. "None", it is used to define a null variable. If "None" is compared with anything else than "None" it will return false.

Keywords in python

These are reserved words that have a specific meaning and purposes and can't be used for anything else but those specific purposes. Note that keywords are different from python built in functions and types. Here are some of the python keywords;

False ,await, else, import, pass,
None, break, except, in, ,raise
True, class, finally, is, return,
and, continue, for, lambda, try,
as, def, from, nonlocal, while,
assert, del, global, not, with,
async, elif, if, or, yield

You can read more about the key words
https://realpython.com/python-keywords/[](url)

Operators in python.

They are different types of python operators;
1. Arithmetic operators


Used to perform mathematical operations like addiction, subtraction, multiplication and division.
Operator Description Syntax
+ - x + y Addition: adds two operands


– - x – y Subtraction: subtracts two operands


* - Multiplication: multiplies two operands x * y


/ - x / y Division (float): divides the first operand by the second


// - x // y Division (floor): The result will be rounded to the next smallest whole number.


% - x % y Modulus: returns the remainder when the x is divided by y


** - x ** y Exponentiation: the result will be x raised to the power of y


>>> x = 4
>>> y = 3
>>> x + y 
7
>>> x - y
1
>>> x * y
12
>>> x / y
1.3333333333333333
>>> x // y
1
>>>x % y 
1
Enter fullscreen mode Exit fullscreen mode

2. Membership operators
Used to test if a sequence with the specified value is present in the object.

in - returns true if the specified sequence is present in the object

not in - returns true if a sequence with the specified value is not present in the project

>>> x = ["pearls", "cherries"]
>>> print("pearls" in x)
True
# return True because the sequence "pearls" is present in the list

>>> print("banal" not in x)
True
# return True because the "banal" is not present in the list

Enter fullscreen mode Exit fullscreen mode

3. Comparison operators
Used to compare two values;
== equal
!= not equal
> greater than
< less than
>= greater than or equal to
<= less than or equal to

4. Logical operators
used to combine conditional statement
and - returns True both of the statements are true
or - return True if one of the of the statement is true
not - reverse the result, it returns False if the results is False

>>> x = 4
>>> print(x > 3 and x < 10)
True 
# returns True 4 is greater than 3 and less than 10

>>> print(x > 3 or x < 3)
True 
# returns True because one of the statement is True

>>> print(not(x > 3 and x < 10))
False
# returns False as it reverses the result.
Enter fullscreen mode Exit fullscreen mode

5. Identity operators
Used to compare objects, not if they are equal but if they are are actually of the same object, with the same memory location.
is - return True if the both variables are of the same objects
is not - returns True if both variables are not of the same objects

>>> x = ["tea", "sugar"]
>>> y = ["tea", "sugar"]
>>> z = x
>>> print(x is z)
True
# returns True because z is the same object as x

>>> print(x is y)
False
# return False because x is not the same object as y, even if they have the same content. To understand this better you can use "==" operator which return True because x is #equal to y
>>> print (x == y)
True
Enter fullscreen mode Exit fullscreen mode

6. Assignment operators
They are used to assign values to variables
=, !=, +=, -=, *=, /=,

πŸ’– πŸ’ͺ πŸ™… 🚩
davidgitonga
David-Gitonga

Posted on April 22, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related