DEV Community

Cover image for A Head Start to Python
Farhan Farooq
Farhan Farooq

Posted on • Updated on

A Head Start to Python

Introduction:

Python is a commonly known programming language now. It supports many programming paradigms. Recently, it is hyped because of data science. lets start our journey.

Print Function and Formation:

In python we use print("Your text you want to display") to display something.

Image description
Image description

Sometimes, we need to declare something in between text or we want to display some value of the variables between a text which we want to print.
For this we need curly braces {} to show the result. Here it is showing how to properly use it.

Image description
Image description

Variables:

There are four types of variables: Integer, long integer, float and string.
In python we do not need to follow any strict rule like other programming languages to declare variables.

Image description

Image description

To know the type of the variable we use type() to know the variable type.

Image description

Image description

Lastly, python variables are case sensitive. Which means if you declare a variable named "a" and another variable named "A" both will indicate two different variable.

Image description

Image description

Strings:

In python strings are like arrays. If a string variable is "STRING" we can iterate through all of the elements of it.

Image description

Image description

Image description

Image description

We can also check the length of string using len().

Image description

Image description

If we want to print something in a range. We need to use slicing. To use slice in we need to put a range. The blueprint for slicing is - variable[start:stop:step]
here,
start = Including the position of start
stop = Excluding(until) the position of stop
step = Jump

Image description

Lists:

There are two kinds of list.
(1) Normal List

Image description

(2) Nested List

Image description

List Indexing:
To access the elements of the list we need list indexing. It is like string indexing for normal list.

Image description

Image description

For nested list we need to first highlight which element and then the highlighted element's element.

Image description

Image description

Length of a list
Using 'len()' we can print the length of the list.

Image description

Image description

Iteration
To access all the elements of a list we need iteration.

Image description

Image description

Slicing
List slicing is like string slicing.

Image description

Image description

Adding Item in a list
To add a new item in a list we have to use 'append()'.

Image description
Image description

List Comprehension
To make the code shorter list comprehension is a very good technique. Here is a example of a list using list comprehension and without using list comprehension.

without using list comprehension

Image description

using list comprehension

Image description

Image description

Top comments (0)