Skip to main content

Posts

Showing posts from March, 2024

Python List

What is a List? List is a data type in Python. List is used to store multiple values ( it can string, integer, float and other lists also ) in a single variable. Lists are mutable, which means you can modify, add, and remove elements from them. We can define a list in the following ways, 1. myList = [value1, value2, value3] 2. myTuple = (value1, value2, value3) myTupleList = list(myTuple) 3. myStr = 'My String' myStrList = list(myStr)  Example: a = [1,2,3] print(a) Output: [1,2,3] You can access individual elements in a list using square brackets and the index of the element. Indexing starts from 0 in the Python List. Like we want to access the third element of the above example list, we can write, a[2] Output: 3 If we want a string in a list, we will use double quotes for the element. Also if we want to use a list in the list we can use it. Example: myList = [2,4, ["a", 4]] myList[2] Output: ["a", 4] List can have duplicate elements. We can add, remove

Data Types

We can identify the type of data/variable based on value. It indicates which type of data that you are using like string, integer, float or other. Data types are classifications of data items, indicating the kind of values they can hold and the operations that can be performed on them. In programming and computer science, data types are crucial because they determine the structure of data and the ways in which it can be manipulated. In Python, data types are dynamically inferred, meaning you don't need to explicitly declare the type of a variable. Python automatically determines the data type based on the value assigned to it. List of Data Types: Name Keyword Example Integer int a = 2 Float float b = 1.2 String str name = "John" Boolean bool is_valid = True List list numList = [10,20,30,40, 50]