Basic Programming in Python - Part 1
4 min read
In this blog, we will discuss the basics of programming using the python language that is widely used in various fields of technology, like Data Science, Neural networks, and Complex computations. The contents of this blog are as shown below:
- Install python
Although most operating systems have python installed by default, you can check if you have it installed by opening a command prompt/terminal and typing' python --version', and hitting enter. If this command returns any version and details, then you are good to go. However, if there is an error saying 'python is not an internal or external command', you can follow the following steps to install python.
- Go to python.org/download
- Click on the download button (by default site has the download button pointing to the respective package compatible with your operating system.
- Follow the steps in wizard with default selected options and continue
- Setting the path in environment variables is an important step, hence make sure to set it up in case the installer has skipped that step.
- That's it. You can now verify from the command prompt.
Variables are called so because they keep varying(changing). You can assign any value to a variable name and you are good to use it in your program. The only rule for variables is not to use any python keywords as variable identifiers. That means, there are a set of keywords used in python language for instructing purposes, such as 'while', 'for', 'if', etc. You cannot name your variables with these keywords. Let us see some examples:
- text = 'This is a text variable'
- name = 'Hashnode is an awesome platform'
- salary = 20000
Numbers is one of python's data type. You can store different number types in number variables. That means integers, fractions (decimal points), long integers. You could even perform different operations on these numbers in different ways. Let us see how to declare and assign a number variable.
Kukatpally_to_Nizampet = 9 #in KM Nizampet_to_Patancheru = 18 #in KM Total_Distance = Kukatpally_to_Nizampet + Nizampet_to_Patancheru print(Total_Distance)
Let me share the result below screenshot:
While printing the variables, you can add text by side as follows:
print('Total distance from Kukatpally to Patancheru', Total_Distance)
In order to get the approx value from a decimal point, we can use the round function as below:
We can also specify the number of decimal points, we want to see in the result, as shown below:
round( 8 - 7.7, 2)
Strings are usually texts. We can use both single quotes and double quotes for indicating a string. We can use triple quotes ''' to wrap multi-line strings, for example in the case of specifying the address of a user. However, in case of having an apostrophe in the text, it is recommended to wrap the text with double quotes as python would recognize the apostrophe as the end of the string. Let us check this in below examples:
If you observe below, the Jupiter notebook has specified how python is reading the code.
Hence, the right way of declaring above string is :
Let us now discuss, how python stores strings. Usually, a char occupies 1 byte of memory and for strings, if we consider each character as an individual element, python stores them as an array of elements.
Say we have a string (platform = 'Hashnode is awesome'), python stores it as shown below:
| H | a | s | h | n | o | d | e | | i | s | | a | w | e | s | o | m | e |
And like any other array, the start index is 0 (zero). This allows us to extract substrings and replace characters at a specified index, as shown below:
Lists are arrays. List of objects, list of numbers, list of strings, and so on. The start index of an array is zero. The different ways of accessing elements of arrays and different available array functions are shown in the below examples.
- We cannot add two different types, such as we cannot add a number and a string. However, we can convert a number into a string using str() function to achieve this.
- We cannot use the + operator to add an element to the list. Python throws error in such case. However, we can add to lists using + operator.
"straw berry" in stringElements #expected output is False
That's all for this blog. There would be a continuity of this article. Thanks for reading so far. Happy learning.