We always find it difficult to learn a new programming language, and when we start learning a language we always want to learn it completely before start working with it, But is it the best approach to learn, learn it by doing it.
In order to start programming first you have to learn the rule and then get your hands dirty in the ground, in this tutorial we will cover the basics of python programming, and then I will suggest you to start coding for real-world solutions, so you can learn in a better way.
Let's start with the basics of python programming:
When we write a programme we don't want to write something again and again, so we store the value in a more convenient English word, which we can easily remember. for example if you have to enter the value of pie then it's better to enter pie instead of 3.14. Assigning variable in python is very easy, you just have to use a "=" operator. let's learn by doing it.
Look how easy it is, just assign anything with a "=".
So the variable will store the value in the variable name you assigned, and you can access it anywhere.
We also have some datatypes in python we can make use of, here are some basic data types in python.
In almost all programmes we have to write conditions, for example, bring umbrella is its rain, its the simple condition, do this if this happens. we can use it in our code, like checking for the weather forecast, and if it will be a rainy day then send a notification to put an umbrella in the bag. Though we will do this type of stuff later. It was the simple demonstration of how confition statements work, for using a conditional statement in python we make use of "if " keyword.
you can also use else keyword, it works like this "if this happend than do that else do another stuff" here is the code example for this.
Looping / Iterator
Iterating is the basic fundamental concept of any programming language, it basically means for doing things repetitively. In python, we have two keywords to iterating, "for" and "while". The while loop works on conditions, like do this until this happens.
To write a while look we need to have a looping condition. which specify when to do it. and it will iterate till the condition is True, here is another example of it.
Another keyword we use for iteration is for a keyword, in short, it can be "do it for" like connecting it to real life, we have a classroom with 40 students, then we can say, "for a student in classroom ask", it should make all this clear. it basically says that ask to all the students for one by one.
Collections in Python
Sometimes we have to put a number of elements in a collection for better operation, we can say a group of elements, the element can be variable, the name of a student in the class or anything like we can put name of all students in the class in a list.
The list is the basic type of collection in python, then the name is self-explanatory, its a list of some objects. here is the basic example of a list of numbers.
Now we have stored some numbers in a list now the question arise is how we can access it, the elements of the list can be accessed by its index number, the number starts with 0 and goes like this.
you can also use a for loop with a list to iterate through the list.
Lists are mutable, what does it mean if you can update a list even after making it, there are some operations we can perform on lists, like append for adding, and remove for removing, here is the basic example of it.
Dictionary: Key-Value Data Structure
Hash tables are used widely in any programming language, Dictionary is the hashtable in python. In the list you have to get an element by the index number, but here you can access them with a name you assigned. for example if you have some data about a person you can store it like this.
Now to access the data you can use person['name'] and it will return a name of the person, you can also store a dictionary in a list when you have more than one person to store, and iterate through it to get the data.
You can also make an update in a dictionary even after making it like this.
Iterate Through Data Structures
We don't acces the list from the index number for always, there are some easy ways when you can access all the elements of the data structures with a for loop, here is the basic example of how to do it.
So for each name in name list, we printed it.
Coming to the dictionary, we can also iterate through the dictionary like a list like this.
there is another way you can use the iterator in the dictionary. here is how we do it.
Here we have named the variable as key and value, but we can name it anything we want like foo, bar.
Here we have learnt about the basics of python and will cover more advanced python in upcoming tutorials."