Python kickstart : Here are basics that you should know!

What is Python ?

Guido van Rossum developed Python as a programming language in 1991. It is an object-oriented, interpreted, and high-level language that is open source. It has recently gained widespread appeal due to its incredibly powerful general-purpose programming language. Let’s

Variable

The term variable refers to a value. The variable is assigned a value using the assignment statement.
For instance, n = 10, pi = 3.14, day = Thursday.
In this case, n, pi, and day are all variables. Numbers and special characters are not permitted to begin variable names. It has to begin with alphabets. There are no keywords or spaces.

Keywords : and , as , assert, break, class, continue, def, del, elif, else, except, exec, finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or, pass, raise, return, try, while, with, yield, true, false, none

Statement

A statement is a piece of code that the Python interpreter can execute.
While statements, if statements, for statements, and import statements are some examples.

Indentation

Python use indentation to denote a piece of code. In Python, unlike other programming languages, indentation is critical for determining where the code belongs.

Examples:

sensor = 0

                    If sensor == 0:

                              print(“sensor is OFF”)

                     else

print(“sensor is ON”)

Indexing

The index() method searches the list for an element and returns its position/index. There are two kinds of indexing: forward indexing and backward/negative indexing. When the length of the string is unknown, negative indexing is applied.

Examples: Str[0] = ‘H’

                   Str[1] = ‘E’

                   Str[2] = ‘L’

                   Str[4] = ‘O’

Slicing

The slice function returns a subset of characters. We can indicate where the slicing should begin and terminate.

Examples: Str[ : ] = ‘HELLO’

                   Str[0 : ] = ‘HELLO’

                   Str[0 : 2] = ‘HE’ 

                   Str[1 : 4] = ‘ELL’

Datatype Conversion

Data type conversion is the process of converting an object from one data type to another.
Implicit – The Python interpreter does this conversion without the programmer’s interaction.

For example,

(int)a=4     b=0.5 (float)

                 c=a+b = 4.05 (float)

Explicit – This is a user-defined conversion that mandates an expression to be of a certain data type.

For example,

a = 4     b = “5”

    c=a+int (b)

Results for type casting including implicit and explicit

Data Structures

  1. Tuple : It is a collection of objects separated by commas. It can contain duplicate elements. Tuple is immutable and slicing is possible.

Example: tuple = (0,1,2,3,4)

                  tuple[-1] = 4

                  tuple[ : 3] = (0,1,2,3)

                  tuple[ : ] = (0,1,2,3,4)

2. List : A list is a type of variable that stores several things in a single variable. It can contain duplicate elements.

Lists are mutable and slicing is possible.

Example: list=[0,1,2,3,4]

                  list[0]=0

                  list[0 : ] =[0,1,2,3,4]

3. Sets : It is used to keep several elements in a single variable.. It cannot contain duplicate elements. Sets are mutable and slicing is not possible.

Example: set1 = {0,1,2,3,7,5,9}

                 Set2 = {5,9,0}

4. Dictionary : It is used to store data in the form of a key:value pair. They cannot have multiple keys, but they can have duplicate values. Dictionaries can be changed, however slicing is not feasible.

Example:  dict = { 1 : ‘hello , 2 : ‘world’ }

Functions :

A function is a reusable set of programming statements that is designed to execute a certain purpose.

  • User-defined function: A group of functions defined by the user to do a certain task.
  • Built-in function: These functions are used to accomplish a common activity.
  • Lambda function: It’s a function that does not need to be named. In Python, a lambda expression allows the user to create anonymous functions by using the keyword ‘lambda’.

To refer the latest Python documentation, you can access it here.