Python Tutorial(Part-1)-Introduction
Python is an easy to understand high-level programming language. If you want to switch programming language or learn your first programming language python is the way to go.
Why python
fewer lines of codes as compared to other languages
beginner friendly(easy to understand)
used in many domains like web development, machine learning, data science, deep learning and many more
Getting started
Python installation is a bit different for different operating system. You can go to python.org/downloads and easily install latest or a suitable version of python. Also, you can use whatever text editor you like it does not matter.
Lets code
Writing hello world in python is super easy
>>>print("hello world")
hello world
Variables
Variables in python can be assigned by assigning a value to it.
>>>x = 5
>>>y = "hey"
>>>print(x)
5
>>>print(y)
hey
Indentation, comments and case sensitivity
Before we learn anything else we should know above mentioned things
Comments:
To comment a code or anything put a # before it like
# this is a comment
Indentation:
In other languages indentation is to make code look good and aesthetic but in python it will throw error without proper indentation.
>>>if x>y:
print(y)
Case Sensitive:
Python is a case sensitive language i.e. Aaaa and AAaa are two different things.
Data types in python
There are four built in collection data types in python(arrays):
- Lists
- Tuples
- Sets
- Dictionaries
Lists and Tuples
Lists and tuples are built in collection data types in python with a difference that tuples are immutable and lists are not. It means that content of a list can be changed once it's made which is not the case with tuples.
>>>list = ["a", "b", "b", 1]
>>>tuple = ("a", "a", 4, "c")
Lists are very useful as they can be use as arrays, stacks or queue.
Loops
for loop
for loop is used for iteration.
>>>pet_list = ("dog", "cat", "hamster")
>>>for pet in pet_list:
print(pet)
dog
cat
hamster
In the above code we iterated through a list using for loop
while loop
while loop is used when we want to perform a command as long as a condition is true.
>>>a = 0
>>>while a<3:
a+=1
print(hey)
hey
hey
hey
Sets and dictionaries
Set
It is one of the four built in collection data types in python. It does not allow duplicates.
set= {"item1", "item2", "item3"}
Dictionaries
Like sets, dictionaries also don't allow duplicates. They store data in key-value pairs.
dict = {"name": "bob", "age": 25, "weight": 80 }
dictionaries can also be used to make hash-map data structure
In this tutorial we just got introduced with basic syntax of python programming language. In coming blogs we will learn python conditions, classes and take a deep dive in contents of this blog.