Posts

Showing posts from May, 2020

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 =...

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 sectio...