Fundamentals of Python Programming with Examples

We saw a lot of basic concepts like types of operators, jump statements, and control structures. Now we move ahead to the fundamentals of python. In the fundamental concepts, we will see, what is lists, tuples, sets, and dictionary and their advanced properties, and also we will discuss functions.

Data Types

Python programming language has four important collections of data types such as List, Tuples, Set, and Dictionary.

  • List
  • Tuples
  • Set
  • Dictionary

List

A list in Python is known as a “sequence data type” like strings. It is an ordered collection of values enclosed within square brackets [ ]. Each value of a list is called an element. It can be of any type such as numbers, characters, strings, and even nested lists as well.

A-List is an ordered, indexed, and sequence data type. A list is a mutable data type (changeable). The elements can be modified or mutable which means the elements can be replaced, added, or removed.

Syntax of List:

variable=[element1, element2, element3]

python virtual image

In the above example, we saw a both list and nested list and I used one built-in keyword called type(), which returns a data type of a variable.

Tuples

Tuples consist of a number of values separated by a comma and enclosed within parentheses(). A tuple is similar to a list, values in a list can be changed but not in a tuple.

A Tuples is also an ordered, indexed, and sequence data type. A list is an immutable data type (unchangeable). The elements cannot be modified.

Syntax of tuples

  • variable=(element,) # This for single element tuple
  • variable=(element1, element2, element3)

Difference between List and Tuple

  • The elements of a list are changeable (mutable) whereas the elements of a tuple are unchangeable (immutable), this is the key difference between tuples and list.
  • .The elements of a list are enclosed within [ ] square brackets. But, the elements of a tuple are enclosed by ( ) parentheses.

In the above example, we can see a lot of difference in tuples.

Set

A set is another type of collection data type. A Set is a mutable and unordered collection of elements without duplicates. That means the elements within a set cannot be repeated.

A set is created by placing all the elements separated by a comma within a pair of curly brackets { }.

Syntax of set:

variable={element1,element2,element3}

python virtual image

the python, Set { } is supported the four-set operations such as Union, Intersection, Difference, and Symmetric difference. we will see in upcoming blogs.

Dictionary

A dictionary is a mixed collection of elements. Unlike other collection data types such as a list or tuple, the dictionary type stores a key along with its element. It also a mutable data type.

.The keys in a Python dictionary are separated by a colon ( : ) while the commas work as a separator for the elements. The key-value pairs are enclosed with curly braces { }.

Syntax of Dictionary:

Dict_Name = {key_1:val_1, key_2:val_2,key_3:val_3}

python virtual image

The above example is a basic dictionary program. we will see Advance of List, Tuple, Set, Dictionary in upcoming blogs. if you had any confusion help read again and again until you are satisfied.

My Previous Post

Jump in Python: https://iterathon.tech//jump-statement-in-python-with-examples/

LEARN LITTLE BY LITTLE TO CHANGE THE TECH WORLD

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *