Python — 2

Himashi Karunathilake
3 min readNov 29, 2019

--

Hi guys… 😍

This post will be a continuation of my previous post on Python that focused on the output statement and variables ( https://himashikarunathilake.medium.com/python-1-ca8f34d2ee79 ). So lets get coding!

Lists

Lists are similar to arrays and can contain as many variables as necessary. In order to declare a list “fruits”:

If the elements have to be printed easily, the following piece of coding can be used:

Declare a list, add elements to it and print them

Basic Operators

The arithmetic operators in Python is pretty much the same as with other languages:
+ → addition
- → subtraction
* → multiplication
/ → division
% → remainder

If a power relation has to be created, we can use **.
e.g., 5 ** 2 → means 5 to the power 2 (squared)
6 ** 3 → means 6 to the power 3 (cubed)

In the previous post we discussed how + is used for concatenating strings. Similarly, * can be used for multiplying a string for a number of times.
e.g., zoo = “monkeys “ * 100 → This statement will type “monkeys “ for a 100 times.

Lists can also be concatenated with + sign, where what happens is that all elements in the involved lists are combined to create a new , lengthened list.

Lists can also be used for repeating a string for a number of times using * sign.
e.g., print([1, 2, 3] * 5) → This statement would repeat the given set for 5 times.

Some arithmetic operations

String Formatting

In the previous post also we discussed a few conversion specifiers. They are also known as “argument specifiers”. Here is a summary on some basic argument specifiers:

Basic String Operations

***** Please note that indexing in Python, like most languages, start from 0, which means that if we need the 4th letter, we must give the index as 3 (0, 1, 2, 3 ). *****

***** If indexing starts from the end of a string, it begins from -1. *****

The following is a summary on the operations on strings that can be done:

So guys, that’s it for this post. I hope it helped you to develop your knowledge on Python further. Please try out the tutorial and check your answers from folder Python — 2 in https://github.com/Himashi-Karunathilake/Python and do your own research too. Till I meet you on the next one, Happy Coding! 🥰

Ciao… 👋

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

--

--

Himashi Karunathilake
Himashi Karunathilake

Written by 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!

No responses yet