Python — 1

Himashi Karunathilake
5 min readNov 28, 2019

Hi guys… 😍

From today, I would like to start a series of of posts where I bring to you the basics of learning the programming language — Python.

Introduction

Python is an interpreted, high-level, open source and general purpose programming language created by Guido van Rossum which is also cross platform. It is a language that provides object-oriented programming with many file extensions like .py and .pyi being some examples.

To be used, Python should be initially installed in your operating system. It can be downloaded via https://www.python.org/ and here is a step by step guide as to how to install it based on your respective operative system: https://realpython.com/installing-python/ .

By installing Python you gain access to PyCharm which is an IDE for Python language. You can now install the open source, community edition of PyCharm to start coding! Although you can use PyCharm, I will be using Visual Studio Code (download: https://code.visualstudio.com/ ) after installing the necessary extensions.

Please note that I will be using Python 3 and codes for previous versions are different.

So, now let’s get started!

The Output Statement

If you have studied any other programming language before, you might know the Universal Statement that we all code before we begin actual coding. 😂 As an introduction to the language, we always code an output statement, that when run, will provide us with the statement, Hello World! . So, let us code just that as our first step.

Initially, I will open a new file in Visual Studio Code and save it as helloworld.py . The output statement will be print(“insert text here”). Therefore, in order to get the output as Hello World!, we can type the following code and run the script:

Comments in Python begins with #.

The top right line shows the “run” button that needs to be clicked after coding to run the script; the top left line shows the code; the bottom left line shows the output statement

Variables

*** In most of the programming languages, to show that a certain section of coding belongs to a particular block, curly braces are used. But, in Python, instead of the curly braces, that particular section is indented using spaces (standard: 4 spaces) or a tab space. ***

Since Python is completely object oriented, there is no need to declare a variable before using it. Here, every variable is an object. Python mainly supports integers and floating point numbers. It also supports complex numbers. Here, we will not be focusing on complex numbers.

Assume that you need to have a variable called “age” and assign your age to it and then print it. You can simply do it as follows:

The created integer variable with its output is shown

There are two ways to create a floating point number. They are mentioned below:

The two methods to create floating point variables are shown with their respective outputs

String variables are defined with either single quotes or double quotes. It is recommended to use double quotes since usage of the other might eliminate apostrophes if used.

Various ways of printing strings

Some important points:

1 — If you want to use apostrophes with single quotes, use \ before the apostrophe.
e.g., ‘She doesn\’t listen to the teacher.’
2 — If double quotes have to be used within double quotes, again use \ before each quote.
e.g., “\”Yes, you are discharged,\” said the doctor.”
3 — If a new line has to be inserted within a print(), use \n characters.
e.g., print(“Yours Sincerely,\nMary Jane”)
4 — Values to variables can be assigned simultaneously, but keep in mind that the order of the assignment should be with accordance to the variables created.
e.g., a, b = 1, 2
Here, a = 1 and b = 2

Suppose you have to concatenate two strings, you can use the + sign. If used with numbers, this sign would simply add the numbers to give the total.

Concatenation
Concatenating between various variable types

The above output statement can be written in a single statement as follows:

So guys, that’s it for this post. The scripts used in this post alongside with a tutorial for this post can be accessed via folder Python — 1 in https://github.com/Himashi-Karunathilake/Python .

Try out the tutorial and check it with the answers given and do your own research too. I’ll see you in the next installment of this post. Till then, Happy Coding!

Ciao… 👋

Originally published at http://mysnowfrostpersonal.wordpress.com on November 28, 2019.

--

--

Himashi Karunathilake

I am a cybersecurity enthusiast and writer with a passion for demystifying complex topics. Join me as I explore the ever-evolving world of cybersecurity!