We can add elements to a list using the append method. The
append() method adds a single element towards the end of a list.
For example, let’s extend the string by adding “April” to the list with the method
append() will increase the length of the list by 1.
list.append() adds a single element to a list
Tuples are made of several items, just like a list, but they cannot be modified in any way. They are widely used internally in many of the systems we depend on, like databases. Tuples hold data in the order we supply it, & we can access the elements inside a tuple with an index.
For example, if we have a tuple like
('chocolate chip cookies', 15) & we want to access each part of the data, we can use an index just like a list. Tuples are easier to process & more memory efficient than lists.
They are immutable, which…
Python string method
split() inputs a string value & outputs a list of words contained within the string by separating or splitting the words on all the whitespaces by default. It also has an optional argument for limiting the number of splits.
split() method converts the string into other data types like
lists by breaking a string into pieces. Python provides us with two methods:
.rsplit() take a separating element by which we are splitting the string, & a maxsplit tells us the maximum number of substrings we want.
if…elif…else are conditional statements that provide us with the decision making that is required when we want to execute code based on a particular condition.
if…elif…else statement used in Python helps automate that decision making process.
if condition is considered the simplest of the three & makes a decision based on whether the condition is true or not. If the condition is true, it prints out the indented expression. If the condition is false, it skips printing the indented expression.
In Python, we can use a list function which creates a collection that can be manipulated for our analysis. This collection of data is called a list object.
While all methods are functions in Python, not all functions are methods. There is a key difference between functions & methods in Python. Functions take objects as inputs. Methods in contrast act on objects.
Python offers the following list functions:
Numpy’s random module, a suite of functions based on pseudorandom number generation. Random means something that can not be predicted logically.
In this example, we will stimulate a coin flip. We will use the function np.random(), which draws a number between 0 & 1 such that all numbers in this interval are equally likely to occur.
If the number we draw is less than 0.5, which has a 50% chance of happening, we say heads & tails otherwise. This type of result where results are either True (Heads) or False (Tails) is referred to as Bernoulli trial.
The pseudorandom number…
Python’s rename column is a method used to change the column nambes with Pandas’ rename function. It’s useful when we load a tabular dataset that has no column names or if we want to assign different names to specific columns.
Its syntax is given as:
When working with data, we are often going to need to count items, create dictionaries values before we know keys to store them in, or maintain order in a dictionary.
Counter is a powerful tool for counting, validating, & learning more about the elements within a dataset that is found in the
collections module. We pass an iterable (list, set, tuple) or a dictionary to the
Counter. We can also use the
Counter object similarly to a dictionary with key/value assignment, for example,
counter[key] = value.
A common usage for
Counter is checking data for consistency prior to using it.
Just like in mathematics, programming languages like Python have operators. We can think of them as extremely simple functions that lie at the basis of computer science. These are the simplest operations to which a computer program can be reduced. They are essential knowledge for any aspiring Data Scientist or Software Engineer.
In this tutorial, we’ll learn about:
These will probably me the most familiar with us, since they come from basic mathematics.
The arithmetic operators are addition (
+), subtraction (
-), multiplication (
*), division (
What Is String Formatting?
String formatting is attractively designing our string using formatting techniques provided by the particular programming language. We have different string formatting techniques in Python. We are now going to explore the new f-string formatting technique.
f-string evaluates at runtime of the program. It’s swift compared to the previous methods.
f-string having an easy syntax compared to previous string formatting techniques of Python. We will look into every bit of this formatting using different examples.
Every f-string statement consists of two parts, one is character f or F, & the next one is a string which…