Saturday, November 16, 2024
Google search engine
HomeData Modelling & AIPython’s Parts of Speech: Basic components of Python

Python’s Parts of Speech: Basic components of Python

This article was published as a part of the Data Science Blogathon

Introduction

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.

1. Variables (Noun in English)

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.

a. Integer

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:’)



b. Float

Floating-point numbers are real numbers with a decimal point.

Example: 10.1, 35.7,67.90

c. Complex

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

d. String

The string consists of one or more characters enclosed in single, double, or triple quotes.

Example: str1= ‘This is Python Programming’

string | components of Python

As discussed earlier, every type in itself is an object. These objects have associated attributes and methods.

components of Python | string2

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

 
components of Python | float

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.

2. Collections (Adjectives in English)

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.

a. Lists

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

lists

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.

implement lists | components of Python

b. Tuple

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

 

tuple | components of Python

 

c. Set

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}

set

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.

set1

 

d. Dictionary

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’

}

dictionary
dictionary 2

Methods keys() and values() can be used to access dictionary items.

3. Loops

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.

a. While

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

while | components of Python

The above while-loop prints numbers 1 to 10 as the while condition fails when the value of x is more than 10.

b. for

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.

for loop

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.

i) break

Break statement stops the execution and exits from the loop

break

The loop stops execution when the value of x reaches 30.

ii. continue

Continue statement suspends the execution of the current iteration and starts from the next one.  

components

The program does not execute the print statement whenever the if-condition is met. It prints only the odd numbers in the list.

iii. pass

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.

pass

The program continues without throwing any error.

4. Conditional Statements

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. Simple if

A set of instructions is executed only if the specified condition is met in the program

 

if | components of Python

The print statement is executed only if the value of ‘x’ is greater than 10.

b. If ….else

An if-else statement offers an alternative to execute when the if condition is not met.

if else

The else statement has an alternate print statement to execute when ‘x’ is not greater than 10.

c. if …elif..else

This statement supports a series of conditions to be checked before the execution of a statement.

components of Python | if elif else

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.

About Me:

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.

Chaitra B V

31 Aug 2022

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments