Python, Python, and Python... If you are an IT or CSE student you must have heard this word a lot...
Python is now the most favorite programming language among the new generation IT guys. If you want to get success in IT, you surely need a toolbox of various cool programming languages and a lot more skills, And python is one of the must-have tools you should have in your toolbox.
You know, Python has an amazing collection of libraries that every programmer needs or dreamed of. Moreover, python is such a friendly language that it can support other programming languages to write in the script. weather it is web development or An App development, python will always make your task simple.
Let's look at some examples.
Python can handle your complex calculation within the speed of light. Say you want to multiply 576 and 456. it's simple in python, just type
```python 576*456 ``` 262656 Wooh! it's done. The * operator is used to perform the multiplication. Now, let's say you want to add them ```python 576+456 ``` 1032 Okay, let's divide ```python 576/456 ``` 1.263157894736842 mmm... how to do power operation? You will wonder.. it is so simple.. let's find out what is 2 power 8 ```python 2**8 ``` 256 Observe, how the double star ** operator is different than *. Okay how will you perform square roots? yes, you guessed it write. ```python 36**(1/2) ``` 6.0 simple... huh... now, that you are familier with numbers to work in python, let's look how we can do the same with texts. ```python "hey user how are you?" ``` 'hey user how are you?' we call the above text, string in python. Any text sourrounded within " " is considered as string. In python you can also use single quotes like 'hey how are you'. We will deal with strings in further posts, but for now I just want to introduce to you how we can represent the texts.
Data Types In Python
Ohh... one more topic you should know is of data types.
What are data types? We use different types of data in our day to day life, To make our computer make understand these data we use the concept of datatypes. In python, there is a function called type() use to know the data type. Let's look at some examples... ```python type(8) ``` int int, it is short for integers. ```python type(8.7) ``` float float, it is the datatype for decimal values. ```python type("hello folks") ``` str str, it is the short for string datatypes
Variables In Python
you can use type() to know the datatype of any variables in you script. Oops,what is variables? Oh, its nothing big deal. variables are like containers or storage for you data. See how we use the numbers 576 and 453. what if we want to use them further in our script? we need to store them right? ```python a = 576 b = 453 ``` Now, i can use these variables instead of these number to perform any calculations. ```python print(a*b) print(a+b) print(a/b) ``` 260928 1029 1.271523178807947 Python denifes several restriction to name a variables. These restriction are neccessary so that the python interpreter not get confused in between reserve keywords and variables. suppose you name a variable as "def" then python will raise an error because def is a keyword use to define you own function. Don't worry if you dont know about functions we will deal with in later posts. 1. A variable name should not be same as any reserve keywords. 2. A variable name should not start with numbers or any special character other than _ One more thing you should keep in mind that python is case sensitive while naming the variables. For example, Name is different than name and so is NAME. Let's look at some examples ```python _i_am_valid = "what's up yo yo boy honey singh" print(_i_am_valid) ``` what's up yo yo boy honey singh ```python 2_i_am_not_valid = "errors" ``` File "", line 1 2_i_am_not_valid = "errors" ^ SyntaxError: invalid token Ooops... you got your first error!! Just don't worry. error are very helpful when you start understanding them. In our case we got an SyntaxError which says invalid token! This means that variable name or token is not a valid name. Why? yes, you guessed it right, it's because it starts with number. ```python hey$Iam variable = "hello" ``` File " ", line 1 hey$Iam variable = "hello" ^ SyntaxError: invalid syntax Hmmm, another! but why? yes, we used an special character $
Comments In Python
One more basic concept before we dive deeper is of comments. Commments is nothing but used as an explaination for a syntax or code we write. It's a good practice to add comments to explain what you code is going to do. it help you as well as every other programmer who read you code. Let's look at the example how we can use the comments ```python r = 4 # this variable is use to store the radius of a circle area = 3.14*r**2 print(area) ``` 50.24 ```python ``` # Interesting Stuffs Ahead !!! Remember the mathematical operators we used in above examples? what happens if we use them with strings? Let's see!! ```python "hello "*5 ``` 'hello hello hello hello hello ' Amazing right!!! If you want to print some text over and over again, you can use this trick. ```python "hey " + "You beautyfull" ``` 'hey You beautyfull' Observe how the + operator use to combine to strings. that is the beauty of python. What if we use one string and one number? ```python "Hello" + 78 ``` --------------------------------------------------------------------------- TypeError Traceback (most recent call last)in ----> 1 "Hello" + 78 TypeError: can only concatenate str (not "int") to str Well, here you got an very important error. Just read what it says, TypeError: can only concatenate str (not "int") to str It means that + operator with string can use only another with sting datatype. Now, i think you got a preety good idea about how to name variables. Just don't start with numbers and don't use any special characters. You can only use _ (underscores) So far we have covered the basics foundation you should know before starting python. Observe we did not use any ; or any other complex things we use in other programming language. that's the beauty of python. Python say's that you just use your brain to write the logics, i wil handel the remaining complex stuffs for you. isnt it amazing. My job was to make you feel comfortable using the python programming language. just stick to this blog, follow, subscribe we will dive more deep and try to workout with python to do more exciting works. Will meet in next post. Thankyou!!! ```python ```
0 Comments