Skip to main content

Let's Learn Python


Let's Learn Python🐍

Hello everyone.After a long time I am back again with a blog series. In this series no more theory, only practical. Before I say what this series is all about. Let's pray for everyone so that we can overcome this pandemic.

This series is going to be a 3 part series.
  • 1st part- Python Basics 
  • 2nd part - Python Advance and some tricks.
  • 3rd part - Putting all the learning together and creating a project
This is the first part of this tutorial series. I hope you learn something new from this, and at the end of this series you have a project ready with you , which you can put in your CV. If you know Python a bit then you can skip the article, Or you can read for a refresher.


NOTE: Here we are not going to cover everything in one go, we will learn the things throughout the series. So I included only the topics I feel mostly important.

Prerequisite

Install Python in your local Computer. Go to this link and download 3.6 or above.

After Installing Set your environment variables properly.(for Windows)
If you are using linux then python is already installed in your OS. Just open terminal and write python3 to see the version installed.

Python

Before learning something new you should always ask a question to yourself that Is this what I am interested in? If no then please read this , and I am almost sure your mind may change. And if yes then welcome to this journey and make sure you have your gears ready.
let's start.

Why Python? 

  • Easy Syntax
    Python has a very easy syntax in compare with other programming languages such as Java or C++. No I am not saying learning those is bad or something, but if you are someone who wants to build a lot of stuffs and also stick to a single programming language, my dear python can do that, and again you can easily learn it even if you are a absolute beginner in programming.
  • OOP Support
    Python supports Object Oriented Programming Structure, if you are familiar with Java then you might know , how beneficial it is to use such a language which can support OOPs concepts. We won't discuss about OOPs more in this article, as I decided to discuss it in the second part of the series.

  • Easy to Create Stuffs
    What do you like? Web Development? Machine Learning or just Numerical Computing,
    nah you might like plotting data and visualize it, or you might just love to automate tasks like sending a mail, message or building a chatbot. So my dear fellow passenger learn python and you can fly on your own. 
Okay enough of theory , now let's do some interesting stuff i.e coding. 😁


Hello Python

Go to your windows search option and type IDLE. IDLE is the coding editor that comes with python for windows, but you can use your favorite coding editor if you like. If you want to know what are the cool editors out there which you can use please check my this article.
After you open IDLE you can see something like this

IDLE
IDLE Python Interpreter

This is called the IDLE python interpreter or shell. Here we will code our first program i.e printing the message Hello Python. So without wasting much time let's start

hello_python
Printing Hello Python

Yes only print(Hello Python) is enough for printing . No imports no main function(for now we will talk about this main function in the next part). Using a simple function print() which can print the message. Pretty Cool right? This is just the beginning my dear.

Data Types

For the Hello Python we used the python interpreter directly, we can do that, but when we are creating something large we might have to create a file and then run that file. You can play with the python interpreter by typing 2+2 or 4*4 or 5**5 this all evaluate to something like this. Yes It can do this also.
NOTE: ** is the power operator.

play interpeter
Some Interpreter stuffs.

But Now we will Create a new file and edit that. Just press Ctrl+N and a new window will Open. This is the new file. 
Now like any other programming language python also as datatypes . But here is a magical thing you don't need to declare the data types explicitly. like This

data types
Data Types

Yes like this you can define data in python , integer and double both are consider as numbers in python, character and strings both are consider as strings and for conditional checking we can use boolean or bool data types , which have only two values either true or false. Now to run this file first save the file using Ctrl+S  name it as data_types.py in a desired folder. then press F5 or go to the Run option in the menu and select run module. after that you can see this output in your screen.

data types
Data Types Output

Operators

In Python there are operators like other languages. Create a new file as operatos.py and start writing. The statements after the # sign is called comments and the are ignored by the interpreter. There are other ways of commenting also, this is called single line comment.

operators
Arithmetic Operators


operators
Arithmetic Operator Output

There are few other operators also . like Assignment operators and Conditional operators.

Assignment Operators are for assigning values easily.
Assignment Operators


Assignment Operator Output

Conditional Operators used to evaluate if a condition is true or false , thus it returns boolean as True or false, like this.


Conditional Operators

Conditional Operator Output

Or you can do some cool stuffs like this with operators. 

Cool Stuff with Operators

Yes you can print strings like this in python. cool right?


Conditions

In any programming language we have condition checking .Python also has condition checking as if-else, if-elif.
let's write some code and see how this condition checking works. Create another file as if-else.py and write the following code.

If -else

If else output

This is a simple program where we are checking the number given by user is five or not. look at the first line here we used two new functions input() and int() . input() is  used to take input from user and the quoted text inside input() function is the text which will prompt the user. int() is a typecasting function here input() takes the values as string but we are comparing int values so we need to typecast the string to int using int() function. Now look at the if-else structure . If the condition inside if() is true(remember true as boolean data type) then the program transfer the control inside if(),
and if false then inside else . Now look closely ,in python we don't have {} curly braces to decide the scope, instead we have : operator along with tab to decide the scope of current statement. Now you might ask what this scope means? In a easy way you can say in this program scope of if is up to print("a is five") i.e scope means the block of code inside if.
There is another condition checking mechanism known as if-elif structure. 


if-elif 


if -elif output



so in if-elif we have another condition checking, as you can see here in elif block again we checked the value of a , and if it is 4 we printed a is four. I think with this you get the idea how to use condition checking in python. 

NOTE: If you are from C,C++ or Java you might be wondering hey where is the switch condition , sorry dear no switch -case in python.But there are some ways to do the same.

Loops

There are mainly two kinds of loops in python 
  • For Loop
  • While Loop
We will start our journey by using for loop.

What is loop? loop is something which happens again and again until a certain condition is true or false. Have you guys watched the movie "Edge of Tomorrow", There Tom Cruise is living the same day until he finished the alpha, Anyways if you haven't watched I recommend you to watch the film. Okay again back to the topic loops, are conditions which will  iterate until something happens and loop condition breaks.

Create a file named loops.py and start writing codes.
let's do something interesting , In this program we will find the average of 10 numbers.
How? Yes using Loops.


For Loop

For Loop Output

Look at the code carefully you will see some new things like range() function and in keyword . For loop and iterate over things which can be iterated ,  For loop consist of this in Keyword. The range() function is a function which takes three arguments(arguments are the things we pass inside a function) start, stop and steps . Now what it does? It generates a list of numbers starting from the start till stop-1, and steps is how much gap will be there in between two numbers. So here we given range(1,11) i.e the function will generate numbers from 1 to 10. fine?
i in range(1,11) is just a way of saying 1 in range(1,11) up to 10 in range(1,11) , you wrote all this in a single line using this for keyword.
I Hope You Understood What are loops for, and how they used.
NOTE: The default value of steps is 1

Now While loop , while loop is exactly does the same but in a different way. like this.


While Loop


While Loop Output

Here in while loop we used a conditional operator
which checking the value of i is less than 11 or not.
in the 2nd line of the code we wrote i=1, so the loop starts iterating from 1, if we don't explicitly define the value of i , the default value is 0. Again inside while loop we are incrementing the value of i by 1,using i=i+1 this statement, thus when value of i becomes 11 then the condition inside the loop becomes false and loop breaks. and The control of the program goest to next line after loop .Thus a while loop works. 

NOTE: Python doesn't support incrementing or decrementing operator like i++ or i-- . You can check if you want.

Conclusion

So with this we are in the end of this part of the tutorial, I hope you learnt something new. In the next part we will learn about 
  •     list,tuple,sets and dictionary 
  •     OOPs concepts
  •     additional  Core Conepts
  •     Some tricks
This is just the beginning of the journey, so the things are are very beginner level, in next article we will dive into some advance and cool concepts.
Till then read and play with the Python shell . 
Happy Coding😇.


Comments

Popular posts from this blog

Problem Solving and Algorithms

Problem Solving and Algorithms Introduction A problem space is all of the various components that go into creating a resolution for a problem. Think of it like a frame, which acts as something of a border to help define an area. A problem space helps you or, on a larger scale, a business, figure out what the problem is, work through ways to correct them and then drives implementation of the appropriate solution. The ultimate purpose is to take corrective action for some identified problem. Problem solving refers to cognitive processing directed at achieving a goal when the problem solver does not initially know a solution method. In computer science there are two different types of problems, ill-defined and well-defined : different approaches are used for each. Well-defined problems have specific end goals and clear expected solutions, while ill-defined problems do not. In computer science, an algorithm is a finite sequence of well-defined , computer-implementable inst...

A Beginner's Guide to System Design

A Beginner's Guide to System Design Introduction Systems design is the process of defining the architecture, modules, interfaces, and data for a system to satisfy specified requirements. Systems design could be seen as the application of systems theory to product development. There is some overlap with the disciplines of systems analysis, systems architecture and systems engineering. Whether you are a backend developer, product manager or technical manager, everyone needs to know the basics of System Design. So this article is the only destination that can solve all queries you have about System Design. Aspects of System Design High Level System Design High-level design ( HLD ) explains the architecture that would be used for developing a software product. The architecture diagram provides an overview of an entire system, identifying the main components that would be developed for the product and their interfaces. The HLD uses possibly nontechnical to mildly te...

A Basic Overview of LINUX

A Basic Overview of LINUX Introduction Linux is a family of open source Unix-like operating systems based on the Linux kernel,an operating system kernel first released on September 17, 1991, by Linus Torvalds . Linux is typically packaged in a Linux distribution.Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project.Popular Linux distributions include Debian , Fedora , and Ubuntu . Commercial distributions include Red Hat Enterprise Linux and SUSE Linux Enterprise Server . Desktop Linux distributions include a windowing system such as X11 or Wayland , and a desktop environment such as GNOME or KDE Plasma . Distributions intended for servers may omit graphics altogether, or include a solution stack such as LAMP . Because Linux is freely redistributed, anyone may create a distribution for any purpose.Linux was originally developed for personal computers based on the Intel x86 architecture, but has ...