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.
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.
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.
To know the type of the variable we use type()
to know the variable type.
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.
Strings:
In python strings are like arrays. If a string variable is "STRING" we can iterate through all of the elements of it.
We can also check the length of string using len()
.
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
Lists:
There are two kinds of list.
(1) Normal List
(2) Nested List
List Indexing:
To access the elements of the list we need list indexing. It is like string indexing for normal list.
For nested list we need to first highlight which element and then the highlighted element's element.
Length of a list
Using 'len()' we can print the length of the list.
Iteration
To access all the elements of a list we need iteration.
Slicing
List slicing is like string slicing.
Adding Item in a list
To add a new item in a list we have to use 'append()'.
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
using list comprehension
Top comments (0)