Let’s talk about Python — Part1
What makes Python a beginner-friendly language? Well, let’s talk about some of its cool features first.
Python has a clean, highly intuitive syntax that makes it expressive i.e easy to write and easy to understand. Another great feature is its portable nature which means we can write a single code that runs on every platform (Windows, Linux, Mac OS, embedded devices, etc).
- Open Source: Python is, and will always be free, and its source code is available to everyone.
- High Level and Interpreted: Python code has English-like syntax and is executed line by line at a time. We do not need to compile our code which in turn makes it a suitable language for quick prototypes.
- Dynamically-typed: As the type of the variable is decided at the run-time, we do not need to specify the data type of a variable. We’ll discuss more on this later.
- Extensive Standard Library: Python provides a large standard library which has a rich set of module and functions. We can simply import these libraries into our code.
If you haven’t installed Python already, go ahead and install it first.
Install the latest version of python on your computer from https://www.python.org/downloads/. You can use your favorite text editors such as Notepad, Sublime Text, Vim, VS code editor, and more. For this article, I’m using a highly configurable & fun text editor i.e Vim. To install Vim, go to https://www.vim.org/download.
To create your first python file in Vim and execute the file, follow the steps mentioned below:
- Open your terminal
vim hello.py(.py is the file format for python scripts) and hit enter
- Go to insert mode by pressing
Let us write some code now. In your editor, type:
Here, the print() function takes some text as input and displays that text on the screen. Here, the text “Hello, World” is the argument that is being passed to the print function.
4. To save your file and exit out of Vim, type
5. To run the program from the terminal, type
Congratulations, you just wrote your very first code in Python! Yayyyy!
Let’s talk more about the line of code we just wrote.
Let’s discuss more on print() function
The print() is a built-in function in python i.e we do not need to import it from anywhere. The parenthesis at the end of the print function tells to execute the function. This will produce an invisible newline character due to which a blank line appears on our screen.
We can use python’s string literal to visualize the following lines:
- New line: It appears when we call print() without any arguments. It is a line comprised of a newline character. It is represented by
- Empty line: It doesn’t contain any character at all, not even a new line. It is represented by
Note: Python uses its
.rstrip() method to remove trailing whitespace at the end of characters. For example,
>>> 'Single line text.\n'.rstrip()
'Single line text.'
We can pass a string literal directly to a print() function.
>>> print("Glad, you're getting better at Python")
In the above example, the sequence of characters inside the “ ” are called strings.
Thanks for reading till the end! Part-2 of this article is coming soon.
Do follow me on Medium and Stay tuned! 🚀