Python is an interpreted high-level object-oriented scripting language. You can learn Python more easily, as compared to other languages, since it frequently uses English words. Also, you will be spared from errors arising due to missing semicolons because Python does not use semicolons! Even better, your code will look nice, no matter how untidily you write code in other languages because you will be forced to indent your code well in Python 😉
In this blog post, you will get an intro to Python, that will help you in laying the foundation for further Python learning.
Intro To Python: The “Hello World” Program
In this section, you will learn how to write and execute a Python program in two different modes.
Interactive Mode Programming
In this mode, you do not have to pass the script file to invoke the interpreter. In the command-line prompt, type the following:
$ python
>>> print “Hello World!”
This will give the following output on the terminal:
Hello World!
Note that Python 3.x does not support the syntax for the print statement as mentioned above. You will have to type:
— print(“Hello World!”)
Script Mode Programming
In this mode, you need to invoke the interpreter by giving a script parameter. The interpreter will start execution of the script and will continue until the script is finished (of course, if no errors are encountered in between). I will give you an example of script mode programming below.
Python files have extension “.py”. Consider a script named “example.py” with the following contents:
print “Hello World!”
I assume here that you have installed Python successfully on your machine. To run our script example.py, type the following in the command prompt:
$ python example.py
The following output will be visible on the terminal:
Hello World!
Forced Indentation
In Python, braces are not used to indicate a block of code. Instead, line indentation is used for this purpose. So, you definitely can’t get away with writing messy code, haha!
For example, consider the following C++ code snippet:
if (month == “January”){
cout << “month is January”;
}
else{
cout << “month is not January”;
}
In Python, you will write the above code snippet as:
if (month == “January”):
print “month is January”
else:
print “month is not January”
Comments
Comments make the code self-explanatory and also help in debugging the code. For writing comments in Python, the hash symbol (#) is used. All the text written after the hash symbol up to the end of the line is considered as a comment by the Python interpreter, and it ignores that text.
An example below will help you to understand comments better. Consider the code snippet below:
print “Hello World!” #This will print Hello World!
The output of the above code is:
Hello World!
The text written after the hash symbol — “This will print Hello World!” — is ignored by the Python interpreter.
Python Lists
List is a very important Python compound data type. It consists of a list of elements separated by commas. Lists in Python are similar to arrays in C, except the difference that the C arrays contain elements of same data types, whereas, the Python lists can contain elements of different data types. Elements of a list are enclosed in square brackets ([ ]).
The starting index in a Python list is 0 and the last index is -1. Lists can be concatenated by using the plus sign (+). For example,
list1 = [‘apple’, ‘mango’, ‘banana’]
list2 = [123, ‘orange’, 789]
print list1 + list2
The output of the above piece of code is as follows:
[‘apple’, ‘mango’, ‘banana’, 123, ‘orange’, 789]
Python Dictionary
Dictionary in Python is similar to hash maps in C++. A dictionary ‘key’ can be of any Python data type, but is generally a string of a number. A ‘value’ can be any Python object. Python dictionaries are enclosed in curly braces ({ }). Dictionaries are unordered — the elements of a dictionary have no ordering of any kind among them.
The piece of code below will help you understand dictionaries better.
sample_dict = {}
sample_dict[‘fruit1’] = “apple”
sample_dict[2] = “banana”
print sample_dict[‘fruit1’]
print sample_dict[2]
The output of above code is:
apple
banana
Python Tuples
Tuples are similar to Lists, however, there are a few differences between them.
- Tuples are enclosed in parentheses ( ( ) ) whereas Lists are enclosed in square brackets ( [ ] ).
- The size of a Tuple cannot be changed whereas the size of a List can be updated
In this blog post, I’ve covered some Python basics. You can learn more about Python by doing online courses, or by going through tutorials online. Happy learning!
Comments are closed.