Comptt

Comptt
  • Home
  • Features
  • _Multi DropDown
  • __Dropdown 1
  • __Dropdown 2
  • __Dropdown 3
  • _ShortCodes
  • _Sitemap
  • _Error Page
  • Documentation
  • _Web Doc
  • _Video Doc
  • Download This Template
February 04, 2023 python

 Lists in Python


What are the lists?

A list in Python is a data structure that stores an ordered collection of items, which can be of any data type, including numbers, strings, and even other lists.
A list can grow or shrink during the execution of the program. Hence it is also known as a dynamic array.
Because of the nature of lists they are commonly used for handling variable-length data.

Lists properties

Lists can be enclosed with square bracket '[ ]' and values inside the list are separated with commas

 fruits = ["apple", "banana", "cherry"]
print(fruits)

//Output
['apple', 'banana', 'cherry']

Python Lists are mutable 

Lists in Python are mutable, meaning the elements within a list can be changed after the list is created.

For example:

 fruits = ["apple", "banana", "cherry"]
print("Before modification: ", fruits)

fruits[1] = "orange"
print("After modification: ", fruits)

//Output
Before modification: ['apple', 'banana', 'cherry']
After modification: ['apple', 'orange', 'cherry']

A list can contain dissimilar types, they are a collection of similar types.

For example:

 li = ["Sanjay", 30, 2.5]
print(li)

//Output
['Sanjay', 30, 2.5]

Items in a list can be repeated, i.e., a list may contain duplicate items. Like printing, * can be used to repeat an element multiple times. An empty list is also feasible.

For example:

 ages = [5, 6, 5, 7, 6]        # duplicates allowed
num = [10]*5 # stores [10, 10, 10, 10, 10]
lst = [] # empty list, valid
print(ages)
print(num)
print(lst)

//Output
[5, 6, 5, 7, 6]
[10, 10, 10, 10, 10]
[]

List slicing

List slicing in Python is a technique to extract a portion of a list, creating a new list that contains only a subset of the original list's elements. Slicing is performed by specifying the start index and the end index separated by a colon ':'.

The syntax for list slicing is as follows:
list[start:end]
Where 'start' is the index of the first element to include in the slice and 'end' is the index of the first element to exclude from the slice. The slice will include all elements from 'start' up to but not including 'end'.

Here are some examples of using list slicing in Python :

  1. Getting a slice of a list:
 fruits = ["apple", "banana", "cherry", "orange", "kiwi"]
print(fruits[1:4])

//Output
['banana', 'cherry', 'orange']
  1. Getting a slice from the beginning of a list:
 fruits = ["apple", "banana", "cherry", "orange", "kiwi"]
print(fruits[:3])

//Output
['apple', 'banana', 'cherry']
  1. Getting a slice until the end of a list:
 fruits = ["apple", "banana", "cherry", "orange", 'kiwi']
print(fruits[2:])

//Output
['cherry', 'orange', 'kiwi']
  1. Getting a slice with a step:
 numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(numbers[1::2])

//Output
[2, 4, 6, 8, 10]
  1. Using negative indexing in a slice:
 fruits = ["apple", "banana", "cherry", "orange", 'kiwi']
print(fruits[-3:-1])

//Output
['cherry', 'orange']

These are just a few basic examples of using list slicing in Python. You can use slicing to extract any portion of a list based on the start and end indices and the step size, allowing you to work with subsets of data easily and efficiently.

List methods

In Python list have some built-in methods that you can use to perform several operations.
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable),
to the end of the current list
index() Returns the index of the first element with the specified value.
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the first item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
Share This:
Facebook Twitter Pinterest Linkedin Whatsapp Whatsapp
python
at February 04, 2023
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Tags python

No comments:

Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments (Atom)

Popular

Facebook

Subscribe Us

Tags

Android-Hacking Code HTML Programming Language python
Powered by Blogger.

Exclusive content in your inbox

Do you want to be creative? Subscribe to Our Newsletter!
* We promise that we don't spam !

* We promise that we don't spam !

Follow us By Email

Get new post updates by email
Powered by follow.it

FOLLOW US BY EMAIL

Get new post updates by email
Powered by follow.it

Report Abuse

Comptt

Search This Blog

  • March 20236
  • February 202315
  • January 202313

Social Plugin

  • facebook
  • whatsapp
  • instagram
  • youtube
  • twitter
  • linkedin
  • email
  • github
Educational Collaborator

Educational Collaborator

Zeal Education Society Narhe, Pune
Educational Collaborator

Educational Collaborator

JSPM Narhe Technical Campus, Narhe, Pune
Project Manager

Project Manager

Mr. Ayushman Sanjay Dalvi | Project Manager for Technicals
Special Collaborator

Special Collaborator

Cyber Security Mumbai | Special Collaborator for Cyber Security and Contents
  • Home
  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Courses
  • Join Us
  • Affiliate
  • Home-icon
  • Courses
  • Programming Lanuages
  • _HTML
  • _JavaScript
  • _Python
  • _Java
  • Programming Notes
  • Compilers
  • Ethical Hacking
  • About Site
  • _About US
  • _Privacy Policy
  • _Cookie Policy
  • _Terms & Conditions
  • _Contact US
  • Join Us
  • Certificate

Menu Footer Widget

  • Home
  • About
  • Privacy Policy
  • Terms & Conditions
  • Contact Us
  • Instagram
  • Join Us
  • Affiliate

Social Plugin

Our honorable CEO and Experts

Founder and CEO and All the Expert Team Members

Courses Provided by Us

You'll discover all of the most up-to-date bring innovative here.

Special Topics

  • Programming Language

Founder and CEO

Mr. Rohan Kumar Bhoi
Hello, this is Rohan Kumar Bhoi, CEO at CompTT. "Develop more skills to be self-dependent! Learn Rapidly, Be Unique, Live Stylish" facebook twitter youtube instagram linkedin github
    Pune, Maharashtra, India

    Latest posts

    10/recent/ticker-posts
    Project Manager

    Project Manager

    Mr. Aayan Sattar Mulla | Project Manager for Formating and Editing
    Honorable Founder and CEO

    Honorable Founder and CEO

    Mr. Rohan Kumar Bhoi

    "Learn new Skills with Us, and Be SELF DEPENDENT"

    With our easy-to-follow tips and tricks, you'll be self-sufficient in no time! Plus, we have solutions to all your computer-related pro…
    "Learn new Skills with Us, and Be SELF DEPENDENT"

    Courses

    • HTML
    Crafted with by Computer Tips-Triks || Distributed by Mr.Rohan Kumar Bhoi || © Copyright 2019-2023 Computer Tips-Triks All Right Reseved
    Design by - Free Blogger Templates | Distributed by Blogger Templates
    Comptt

    Made for WORLD by

    computertipstricks.tech and designed by Mr. Rohan Kumar Bhoi and Team. It is a one of the best Educational Hub for everyone looking for Computer Knowledge.
    • Home
    • Compilers
    • About Us
    • Privacy Policy
    • Courses
    • Join Us
    • Blog
    • Study Material
    • Home
    • About
    • Contact us
    • Privacy Policy
    • Cookie Policy
    • T & C

    Recent in Posts

    3/Ethical%20Hacking/post-list

    Facebook

    Telegram

    Telegram Group
    Comptt

    Popular Posts

    January 25, 2023

    Pages

    • Contact
    • About
    Created By Themes | Distributed By Gooyaabi Template