Today I have learned basic in python

 List

Lists are used to store multiple items in a single variable.

Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are TupleSet, and Dictionary, all with different qualities and usage.

List Items

List items are ordered, changeable, and allow duplicate values.

List items are indexed, the first item has index [0], the second item has index [1] etc.


Ordered

When we say that lists are ordered, it means that the items have a defined order, and that order will not change.

If you add new items to a list, the new items will be placed at the end of the list.

listnew=["apple","banana","grapes"]

listnew.append("orange")

listnew.insert(1,"kiwi")

new1=["mango","cherry"]

listnew.extend(new1)

listnew.remove("apple")

listnew.pop(2)

del listnew[1]

print(listnew)

Output:

['kiwi', 'orange', 'mango', 'cherry']

How to add elements in a tuple in Python?

  • Python program to demonstrate the addition of elements in a Tuple. Initial empty Tuple: () Tuple with the use of String: ('Geeks', 'For') Tuple using List: (1, 2, 4, 5, 6) Tuple with the use of function: ('G', 'e', 'e', 'k', 's') Creating a Tuple with Mixed Datatypes.
  • What is a tuple data type in Python?

    • The built-in tuple data type is probably the most elementary sequence available in Python. Tuples are immutable and can store a fixed number of items. For example, you can use tuples to represent Cartesian coordinates (x, y), RGB colors (red, green, blue), records in a database table (name, age, job), and many other sequences of values.

    • veg=("brinjal","tomato","ladiesfinger")
      (blue,red,green)=veg
      print(blue)
      print(red)
      print(green)
    • Output:

      brinjal
      tomato
      ladiesfinger
    • What is a set in Python?

      • Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Tuple, and Dictionary, all with different qualities and usage. A set is a collection which is unordered, unchangeable*, and unindexed. * Note: Set items are unchangeable, but you can remove items and add new items.
      • What are set operations in Python?

        • Below is a list of the set operations available in Python. Some are performed by operator, some by method, and some by both. The principle outlined above generally applies: where a set is expected, methods will typically accept any iterable as an argument, but operators require actual sets as operands.

        • fruits={"apple","banana","berry"}
          newvar={1,2,3,4,5}
          output=fruits.union(newvar)
          print(output)
        • Output:

          {1, 2, 3, 4, 5, 'banana', 'berry', 'apple'}

Comments

Popular posts from this blog

Building a Full-Stack Student Management System with React.js and NestJS

Today I have solved the problem in javascript

Today I have solved problem in javascript