This article was published as a part of the Data Science Blogathon
A thorough understanding of the basic components of programming language is vital in the development of any code. This article outlines all the fundamental concepts of the Python language.
Just like how parts of speech form a basic building block of the English language, the following components form Python’s parts of speech.
A Noun is the most significant part of the English Language, which identifies a person, place, animal, and a thing by providing a name to it.
Variables act as a noun in Python which provides a name for every string, constant, or numerical value. Variables are containers of the data, which can be accessed, edited, and used throughout the program. In Python, everything is treated as an object. An object is an entity that contains associated attributes and methods. The type of the data is not linked to the variable, instead of the object itself.
Example: x=’Car’
The above variable x represents a String value ‘Car’.
y=10
whereas, variable y represents an integer number.
There are four basic data types supported in Python. Variables are named identifiers for any of these four types of values stored.
Integer consists of positive and negative whole numbers without a decimal point. There are no restrictions on the size of the number in Python.
Example : 10, 35,42 etc…
X=input(‘Enter a number:’)
x = 3
print(type(x))
Floating-point numbers are real numbers with a decimal point.
Example: 10.1, 35.7,67.90
Complex numbers are represented by X+Yj, where X and Y are real numbers and j is the imaginary part. This data type is very useful in scientific calculations.
Example: 3+4j (where j is √-1).
The string consists of one or more characters enclosed in single, double, or triple quotes.
Example: str1= ‘This is Python Programming’
As discussed earlier, every type in itself is an object. These objects have associated attributes and methods.
In the above example, x is an object which is of type complex. The object has attributes ‘real’ and ‘imag’, which give the details of the real and imaginary part of a number.
Now let’s look at a method available for float data type
An attribute describes the characteristics of an object, whereas a method is a function available to an object. Here the function is_integer() check if the variable is an integer or float.
Collections are additional data types in Python required to handle special data values other than the ordinary ones.
Collections can be treated as a container of information just like a variable, but it allows flexibility with the kind of information it can hold.
There are four collection data types in Python.
Lists are ordered collection of series of data of various data types.
Example: Items added to the shopping cart.
list_shopping = [‘Pen’,’Cool Drinks’,’Shirt’,’Toothpaste’]
The above shopping cart items contain a variety of goods required for a household.
In python terms every data is indexed. The index can be treated as a pointer to the information the list holds
Each element is indexed in the order of insertion. In Python Index starts from ‘0’.
A list is an iterable object, which means each data element can be accessed sequentially using a for-loop.
Tuples are ordered collection of data just like a list except, tuples cannot be altered once declared.
Example: Number of days in a week
tuple_days=(‘Mon’,’Tue’,’Wed’,’Thurs’,’Frid’,’Sat’,’Sun’)
Tuples elements can be accessed just like lists using a for-loop
Sets are an unordered collection of data, which does not allow duplicate values.
Example: Id of members who have booked tickets for the film show.
set_users={1234,6789,1245,1567}
The elements of a set can be accessed similar to a list, except the elements are not displayed in the same order in which it was declared.
Dictionaries are ordered collections of key-value data. It does not allow duplication of keys but duplicate values are allowed.
Example: Number and name of passengers who have reserved seats on the train.
Dict_passengers = {
1234 : ‘John’,
1567 : ‘Mary’,
1890 : ‘John’
}
Methods keys() and values() can be used to access dictionary items.
Loops form an important part of any programming language. They are used to execute a set of instructions repeatedly until a condition is met where the loop can be terminated.
There are two main loops in Python.
While loop executes given set of instructions until the condition specified in the loop is satisfied.
Below is the structure of a while loop
while(condition):
statement
The above while-loop prints numbers 1 to 10 as the while condition fails when the value of x is more than 10.
For loop works on sequence data like lists, tuples, sets and execute the given set of statements as long as there are elements in the sequence.
The loop keeps iterating through the sequence until all the elements in the list are printed.
Following are a few special instructions that are used to alter the flow of the loop.
Break statement stops the execution and exits from the loop
The loop stops execution when the value of x reaches 30.
Continue statement suspends the execution of the current iteration and starts from the next one.
The program does not execute the print statement whenever the if-condition is met. It prints only the odd numbers in the list.
Whenever a programmer has a logic yet to be built for a loop but wants the execution of the program to continue without error, pass statements can be used.
The program continues without throwing any error.
Conditional statements form an important part of the Python program where a set of instructions are executed only when a condition is satisfied.
There are three main conditional statements in Python.
A set of instructions is executed only if the specified condition is met in the program
The print statement is executed only if the value of ‘x’ is greater than 10.
An if-else statement offers an alternative to execute when the if condition is not met.
The else statement has an alternate print statement to execute when ‘x’ is not greater than 10.
This statement supports a series of conditions to be checked before the execution of a statement.
The above statement checks the value of ‘x’ if it is greater than 10, else it’s evaluated if it is lesser than 10, finally in the else block it concludes the ‘x’ value to be equal to 10.
Motivated data scientist is passionate about building models that fix problems. Currently on my way towards the transition from Oracle to Data Scientist Domain. Enthusiastic to write blogs and share knowledge and keep learning.
LinkedIn : https://www.linkedin.com/in/chaitra-vijayendra-6aa17478/
The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion.
Great examples. I wrote a couple of bash scripts and now I will try to rewrite them with Python. Your examples been very helpful for me. Thank you keep in touch LinkedIn or email. Claus Blom