Posts

Lists in Python

Image
List in Python is a collection of similar/different data types. Defining a list: A List can be defined/created by enclosing the values within square brackets '[]'. l = [5,7,9,2,4] The above code will create a list with the values given inside the brackets. We can create an empty list by giving just nothing within those square brackets. l = [] Printing a list: We can print the list just by passing the list into the print() function. l = [5,7,9,2,4] print(l)  Output: [5,7,9,2,4]  Creating a list with predefined values: We can create a list with predefined values by using * operator on any list multiplied by n times. l = [0] * 5 print(l)  Output: [0,0,0,0,0] Accessing list elements: We can access the elements of a list by calling the list followed by the index value enclosed in square brackets. l = [5,7,9,2,4] print(l[2])  Output: 9 Since Indices of a list start with 0, Index 0 corresponds to the first element in the list, Index 1

String formatting in Python

Image
Strings are simple! We can just enclose whatever we want into quotes and it becomes a string. But things get complex when we want to create complex strings that may contain many escape characters and variables. In such cases, Python's String formatting functions come to our help. Let us consider an example of a string concatenated with variables name = 'Aaron' city = 'Chennai' s = 'Hey ' + name + ', ' + city + ' Welcomes you' print(s)  Output: Hey Aaron, Chennai Welcomes you Here in Line 3, we have joined various bits of strings and variables using the '+' operator to get the desired output. But this method is so complex and can lead to mistakes. Instead of hardwiring each part of the string, we can use string formatting methods to format our strings effectively the way we want. 1. The str.format() method: This method of formatting strings is more simple than the previous method. In this method, we define the place

Printing and print() function in Python

Printing the output of a program is the first thing we learn while learning a new language. It's because it is one of the most used functionalities in any programming language, and Python is no exception. The most commonly used function to print data is 'print()' which is simple and straightforward. This function provides a lot of options to format the output data according to our requirements. In this post, Let's have a look at all of them in detail. 1. print() function: The 'print()' function is the basic printing function available in Python. Though being Simple, it can do almost everything to tailor the output the way a common user wants to do . To print a statement, just pass the statement within quotes inside the brackets of the print() function. print('Hello World!')  Output: Hello World! We can print data by passing variables into the print function, just like we did previously. One exception is that we don't have to pas

Time Complexity of Basic functions in Python

When working on programs that deal with large amounts of data, optimization becomes very important. The slightest change in the implementation of any workflow could drastically affect the time required by the program. To calculate the time taken to run a program, we need to know the time complexity of algorithms and functions that we are using in the program.  Here let's have a look at the time complexities of some commonly used functions in Python which are li sort() 'in' or 'obj.__contains__(ele) Retrieving an element present at a given index. Time Complexity of Sort() function: Python implements Timsort in its sort() function. Timsort is a hybrid of Insertion Sort and Merge sort which proves to be more efficient in real-life scenarios. It basically utilizes the efficiency of Insertion sort on smaller lists by Breaking down a huge list into smaller parts  Performing Insertion sort on the smaller parts and Performing merge sort on those sorted parts.

Variables in Python

Variables are containers that are used to store data. Python is a dynamically-typed language which means variables can store any type of data such as integers, float, strings, etc. Let us catch a glimpse of variables in this in detail. Declaring variables in Python: We might have seen in languages like C and Java where we have to define a variable with the type of data it will hold before assigning value to the variable. Unlike that, in Python, We don't have to explicitly define variables and their types. To create a variable, just type the name of the variable, use '=' sign, and assign a value.  a = 5 print(a)  The above code sets the value 5 to the variable 'a'. When you print a variable, the value associated with the variable will be printed. Thus the output for the above code will be 5. Just like that, variables can have any type of data. a = 5                b = 10.3            c = 'lowercasepython' d = True               In the

Setting up/Installing Python

Image
Setting up Python is the first thing one should do when learning python. The installation process differs for different operating systems. In this post, We will be looking at Installing Python for every operating system in detail. To create and run any Python program, we need two things  A/Any text editor to write and edit programs(even notepad is enough and can do the job).  The Python Interpreter to execute the programs. We are going to install Python Interpreter first. Note that Python Interpreter comes with its own text editor as a bundled package. So we don't have to worry about text editor. A text editor tailored specifically for a particular programming language combined with its Compiler/Interpreter and all the necessary tools required by a developer is known as Integrated Development Environment(IDE). Installing Python in Windows: Follow the below steps to install Python Interpreter in windows  Go to Python's official website's download section.

Why Python is the Best Programming Language for Beginners?

Image
New to programming? Don't know where to start or what programming language to learn first? Well you've come to the right place. Python is one of the most popular programming languages in the world. The demand for Python programmers is increasing day by day. Python is extremely simple and gives an Easy and Simple Experience for beginners. Listed below are the reasons why Python is well-suited for beginners. 1. Simple Syntax Python's syntax is very simple and straightforward. It's indentation helps in readability of code. One can easily learn the basic syntax within hours. It focuses only on important things and abstracts the complex things which a beginner would have troubles to understand. This increases simplicity of the language. Python's Simple syntax makes it easy to learn 2. Community Support   The popularity of a language is determined by the Community Support it has. When it comes to Community Support, Python is well ahead of it'