Why go for python?
6 months ago I was one of those people who use to hate python due to the indentations ( not a valid point I know but I used to hate due to that ) and to mention Now I am of the opinion that due to the indention readability of code increases making the development process easier. In my college days, I used to work in ruby mostly But due to my job requirements for various modules which needed python I had to learn that.
For Beginners or a person from a nontech background, I think learning Python would be a good choice. Python Syntax is basically like talking and writing plain English. For example, consider this syntax which clearly shows its resemblance to the English language.
Understanding Basic’s of Python
There are 2 versions of the python widely used.
I’ll be covering Python3 because most of the people have ported or have started porting there systems to Python3 environment.
Comments make it easy to write code as it helps in understanding why a particular piece of code is written. Another awesome thing about the comment is that it helps to improve the readability of the code. Well, when you give # ( Single line comment )or “”” “”” ( Multi-line comment ) python understand that’s its a comment and not to execute that line of code.
2. Print Statement
Believe me other than debugging tool from my Vscode anything which has helped me in solving the problem is a print statement. I think a print statement is one of the underrated syntaxes of the programming and you will find that it will be the most helpful when debugging the problems.
3. Declaring Variables
If you have done programming previously you may know variables but for those who are beginners or want a short recap you may read this or skip to the next content. As the name implies, a variable is something which can change. A variable is a way of referring to a memory location used by a computer program.
Well in most of the programming languages you need to assign the type to a variable but in python, you don’t need to. For example in C language for declaring an integer, the following syntax is used
int num = 5; while in python
num = 5 .
Integers as the name suggest they are nothing but a numerical value that can be positive, negative or zero without a decimal point.
Float is the same as an integer but with one slight difference, a numerical value with a decimal place.
Strings are nothing but a formation of characters or integers. In python, strings can be represented using double or single Quotes.
Booleans are constants with either
4. Conditional Statements
Well as the name suggests conditional statements are used to evaluate if a condition is true or false. Many times when you are developing something you need to check a certain condition and depending on that execution needs to done or you need to skip the program execution for that condition or terminate. In such scenarios conditional statements are useful. If, elifandelseare the conditional statements used in python.
Above Snippet Shows declaration of a variable which is mapped to an integer when a value is assigned to it. If a number is equal to 5 then it will execute the print statement 1 or else if a number is less than 5 then it will execute the print statement 2 or if a number is greater than 5 it will execute print statement 3.
5. Iterators or Loops
Sometimes while developing you have a condition which needs to repeat more than once (it’s nature may change while iterating through the loop). For such scenarios, python provides
For loop: Ithelps in iterating over a sequence. A sequence can be a list, a tuple, a dictionary, a set, or a string ( Don’t worry we will cover list, tuple, and dictionary in next topic )
While loop: It will keep on executing till the condition is true.
6. Return Statement
Return statement causes the function to exit and return value back to the caller. It is basically declared when the function has completed the execution and is ready to return the value to the caller. A function can return the output of execution back to the caller.
7. Classes and Objects
Python is an Object-oriented programming language and for those who are new to the programming, there’s procedure-oriented programming which focuses on functions while object-oriented programming language focuses on objects. An object is simply a collection of data i.e variable and methods are used for the execution of the data. A class is a blueprint of that object. For creating a class class keyword is used in python. A simple example of the class is:
As class and objects are broad topics I’ll be covering them in a separate article where I’ll be covering the init method, self parameter, the object method How to modify or delete properties of the object and delete an object.
8. Functions and Methods
Functions are a sequence of statements that are independent of the object. In python, a function is defined using def. They are a block of code and help us in the reusability of the repetitive logic. Functions can be inbuilt and user-defined both. They can have any number of parameters or nothing can be passed to a function. It may or may not return any data.
Methods look same like function only difference is that methods are dependent on the object. A function can be invoked by there name while methods need to be invoked by using their class reference i.e they are defined inside the class.
9. Basic Calculator Program:
Here we have created 4 Functions for addition, subtraction, delete and multiplication. The logic for the calculation is written in the function and a conditional statement is used to parse only the option the user wants. You may seem some different declaration in the program which we have not covered above.
Don’t worry they are just simple syntax for achieving our goal. input(“Select the operations…”) is used for taking the user input. Generally, it gives the output in the string you can check a type of a variable by print(type(user_input)) and our conditions are expecting integer output so we change them into an integer by using type casting. ( int() )
I hope I have made basics of python clear in this article. Some broad topics like classes, objects, data structures and many more will be covered separately. What we learned so far:
- Why go for python? ( I hope, I convinced you to learn Python )
- What are the comments and why are they so important?
- Print statement one of the important tool for debugging.
- Declaring Variables.
- Conditional Statements.
- Iterators or loops.
- What is a return Statement?
- A brief idea about classes and objects.
- Functions, methods, and difference between both of them.
- Basic Calculator Program.
If you liked my work give it a heart or if you think I missed something mention it in the comment section. You can also add a suggestion for covering various future topics in python and frameworks related to the python.
Keep coding let’s be a Pythoner. ( Python + programmer 😜 )