[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"detail-sidebar-cat-0-en-105":3,"doc-seo-697978-105":59,"doc-detail-697978-en":130,"doc-similar-697978":161},{"code":4,"msg":5,"data":6},0,"success",[7,13,18,23,28,33,38,43,48,51,55],{"id":8,"doc_module":4,"doc_module_name":9,"category_name":10,"show_sort_weight":11,"slug":12},1,"Document","Story & Novel",90,"story-novel",{"id":14,"doc_module":4,"doc_module_name":9,"category_name":15,"show_sort_weight":16,"slug":17},2,"Literature",80,"literature",{"id":19,"doc_module":4,"doc_module_name":9,"category_name":20,"show_sort_weight":21,"slug":22},4,"Exam",70,"exam",{"id":24,"doc_module":4,"doc_module_name":9,"category_name":25,"show_sort_weight":26,"slug":27},5,"Comic",60,"comic",{"id":29,"doc_module":4,"doc_module_name":9,"category_name":30,"show_sort_weight":31,"slug":32},6,"Technology",50,"technology",{"id":34,"doc_module":4,"doc_module_name":9,"category_name":35,"show_sort_weight":36,"slug":37},7,"Healthcare",40,"healthcare",{"id":39,"doc_module":4,"doc_module_name":9,"category_name":40,"show_sort_weight":41,"slug":42},8,"Research & Report",30,"research-report",{"id":44,"doc_module":4,"doc_module_name":9,"category_name":45,"show_sort_weight":46,"slug":47},9,"Religion & Spirituality",20,"religion-spirituality",{"id":46,"doc_module":4,"doc_module_name":9,"category_name":49,"show_sort_weight":46,"slug":50},"World Cup","world-cup",{"id":52,"doc_module":4,"doc_module_name":9,"category_name":53,"show_sort_weight":52,"slug":54},10,"Lifestyle","lifestyle",{"id":56,"doc_module":4,"doc_module_name":9,"category_name":57,"show_sort_weight":24,"slug":58},19,"General","general",{"code":4,"msg":60,"data":61},"ok",{"site_id":62,"language":63,"slug":64,"title":65,"keywords":66,"description":67,"schema_data":68,"social_meta":123,"head_meta":125,"extra_data":127,"updated_unix":129},105,"en","lecture-4-tuples-list-slicing-list-comprehension-strings","Lecture 4: Tuples, List Slicing, List Comprehension, Strings","","Lecture 4 explains how Python tuples work as immutable collections, how to create them and access elements using indexing and iteration. It demonstrates the need for a trailing comma for single-element tuples and shows tuple-return patterns from functions, including unpacking and swapping values. The lecture then introduces slicing with the [n:m] syntax across tuples, lists, and strings, clarifying inclusive/exclusive behavior and index positioning.",{"@graph":69,"@context":122},[70,84,105],{"@type":71,"itemListElement":72},"BreadcrumbList",[73,77,79,82],{"item":74,"name":75,"@type":76,"position":8},"https://docshare.wps.com","Home","ListItem",{"item":78,"name":9,"@type":76,"position":14},"https://docshare.wps.com/document/",{"item":80,"name":30,"@type":76,"position":81},"https://docshare.wps.com/document/technology/",3,{"item":83,"name":65,"@type":76,"position":19},"https://docshare.wps.com/document/lecture-4-tuples-list-slicing-list-comprehension-strings/697978/",{"url":83,"name":65,"@type":85,"image":86,"author":91,"headline":65,"publisher":94,"fileFormat":97,"inLanguage":63,"description":67,"dateModified":98,"datePublished":99,"encodingFormat":97,"isAccessibleForFree":100,"interactionStatistic":101},"DigitalDocument",{"url":87,"@type":88,"width":89,"height":90},"https://docshare.wps.com/thumbnails/lecture-4-tuples-list-slicing-list-comprehension-strings/697978.png","ImageObject",300,407,{"name":92,"@type":93},"Liam","Person",{"url":74,"name":95,"@type":96},"DocShare","Organization","application/pdf","2026-11-26","2026-11-12",true,{"@type":102,"interactionType":103,"userInteractionCount":29},"InteractionCounter",{"@type":104},"ViewAction",{"@type":106,"mainEntity":107},"FAQPage",[108,114,118],{"name":109,"@type":110,"acceptedAnswer":111},"What is a Python tuple and how is it different from a list?","Question",{"text":112,"@type":113},"A tuple is an immutable collection of elements. Unlike lists, its elements cannot be changed after creation.","Answer",{"name":115,"@type":110,"acceptedAnswer":116},"How do you create a single-element tuple in Python?",{"text":117,"@type":113},"Write the value with a trailing comma, such as (5,). Without the comma, (5) is treated as a regular value, not a tuple.",{"name":119,"@type":110,"acceptedAnswer":120},"How does slicing with [n:m] work for tuples, lists, and strings?",{"text":121,"@type":113},"The slice [n:m] returns the segment from the n-eth character/index up to but excluding the m-eth. The same slicing concept applies to tuples, lists, and strings.","https://schema.org",{"og:url":83,"og:type":124,"og:title":65,"og:site_name":95,"og:description":67},"article",{"robots":126,"canonical":83},"index,follow",{"doc_id":128,"site_id":62},697978,1794446559,{"code":4,"msg":5,"data":131},{"doc_id":128,"user_id":132,"nickname":92,"user_avatar":133,"doc_module":4,"category_id":29,"category_name":30,"doc_title":65,"doc_description":67,"doc_content":134,"file_id":135,"file_url":136,"file_type":137,"file_size":138,"view_count":29,"is_deleted":4,"is_public":8,"is_downloadable":8,"audit_status":8,"page_count":139,"language":140,"language_code":63,"site_id":62,"html_lang":63,"table_of_contents":141,"faqs":142,"seo_title":143,"seo_description":67,"update_tm":129,"read_time":144,"preview_page_count":19,"preview_urls":145},8796095461564,"https://ap-avatar.wpscdn.com/davatar_155a257f0dc6eb9ab79c44ca47cae57d","Lecture 4: Tuples, list slicing, list comprehension, strings  \nCS5001 / CS5003: Intensive Foundations of Computer Science  \nPDF of this presentation  \n1  \nLecture 4: Announcements  \n We are a bit behind on grading! Very sorry about that--we are trying to work on getting things graded quickly. We will catch up soon!  \n Let's chat about how the class is going.  \n Take a few minutes now and write down three things that are going well, and three things that could be better. Don't worry about being frank about it-- I'd like some real feedback (we want to make the course better!)  \n After you write them down, talk with a neighbor about them. If you agree on one or more items, ﬂag them and bring them up.  \n Let's talk about them as a class  \n I will also have an anonymous feedback site set up so you can put comments there that you don't want to discuss in class  \nLecture 4: Tuples  \nIn Python, a tuple is an immutable (unchangeable) collection of elements, much like a list. Once you create a tuple, you cannot change any of the elements.  \nTo create a tuple, you surround your elements with parentheses:  \n>>> animals = ( 'cat' , 'dog' , 'aardvark' , 'hamster' , 'ermine' )>>> print(animals)  \n( 'cat' , 'dog' , 'aardvark' , 'hamster' , 'ermine' )  \n>>> type(animals)  \n\u003Cclass 'tuple '>  \n>>>  \nTuples act very much like lists--you can iterate over them, and you can access the elements with bracket notation:  \n>>> print(animals[3])  \nhamster  \n>>> for animal in animals:  \n. .. print(f \"Old Macdonald had a/an {animal} ! \" ). ..  \nOld Macdonald had a/an cat!  \nOld Macdonald had a/an dog!  \nOld Macdonald had a/an aardvark!  \nOld Macdonald had a/an hamster!  \nOld Macdonald had a/an ermine !  \n>>>  \n3  \nLecture 4: Tuples  \nIf you don't put the parentheses, but have a comma-separated collection of values, they become a tuple, by default:  \n1 >>> 4 , 5 , 6 , 7 2 (4 , 5 , 6 , 7)  \n3 >>>  \nBecause a tuple is a collection of elements separated by parentheses, you can't simply get a single-value tuple with parentheses, because this acts like a regular value in parentheses:  \n1 >>> singleton = (5)  \n2 >>> print(singleton) 3 5  \n4 >>> type(singleton)  \n5 \u003Cclass 'int '>  \n6 >>> singleton = (5,)  \n7 >>> print(singleton) 8 (5,)  \n9 >>> type(singleton)  \n10 \u003Cclass 'tuple '>  \n11 >>>  \nInstead, you have to put a comma after a single value if you want it to be a single-value tuple, as online 6. Yes, this looks odd, but that's the way you have to do it.  \nNotice that when you print out a single-value tuple, it also puts the comma after the single value, to denote that it is a tuple.  \nLecture 4: Tuples  \nRemember when I said that functions can have only a single return value? Well, they can, but that value could be a tuple:  \n1 >>> def quadratic_equation(a, b, c) :  \n2 ... posX = (-b + math .sqrt(b * b-4 * a * c)) / (2 * a)  \n3 ... negX = (-b-math .sqrt(b * b-4 * a * c)) / (2 * a)  \n4 ... return posX, negX 5 ...  \n6 >>> import math  \n7 >>> quadratic_equation(6 , 11 , -35) 8 (1.6666666666666667 , -3.5)  \n9 >>> x1, x2 = quadratic_equation(5 , -2 , -9)  \n10 >>> print(f\"The two solutions to the quadratic equation for 5x^2-2x-9 are: \"  \n11 f\"{x1} and {x2} . \" )  \n12 The two solutions to the quadratic equation for 5x^2-2x-9 are:  \n13 1.5564659966250536 and-1.1564659966250537 .  \n14 >>>  \nNotice that you can return a tuple by simply returning two ( or more) values separated by commas, as in line 4 above.  \nYou can capture tuple return values separately into variables, as in line 9 above.  \nLecture 4: Tuples  \nAs shown on the last slide, you can capture the values of a tuple with a comma separated list of variables:  \n1 >>> inner_planets = [ 'Mercury' , 2 >>> mercury, venus, earth, mars 3 >>> print(mercury)  \n4 Mercury  \n'Venus' , 'Earth' , 'Mars' ]= inner_planets  \nThis ability allows you to do some interesting things with tuples. What does the following do?  \n1 >>> x = 5  \n2 >>> y = 12  \n3 >>> x, y = y, x  \nLecture 4: Tuples  \nAs shown on the last slide, you can capt","cbCaikM4YAVPAKfv","https://ap.wps.com/l/cbCaikM4YAVPAKfv","pdf",459820,23,"English","# Lecture 4: Tuples, list slicing, list comprehension, strings\n## Tuples in Python\n## Single-element tuples and tuple returns\n## Unpacking and swapping with tuples\n## Variadic arguments with *args\n## Slice syntax for tuples, lists, and strings","[{\"question\":\"What is a Python tuple and how is it different from a list?\",\"answer\":\"A tuple is an immutable collection of elements. Unlike lists, its elements cannot be changed after creation.\"},{\"question\":\"How do you create a single-element tuple in Python?\",\"answer\":\"Write the value with a trailing comma, such as (5,). Without the comma, (5) is treated as a regular value, not a tuple.\"},{\"question\":\"How does slicing with [n:m] work for tuples, lists, and strings?\",\"answer\":\"The slice [n:m] returns the segment from the n-eth character/index up to but excluding the m-eth. The same slicing concept applies to tuples, lists, and strings.\"}]","Lecture 4: Tuples, List Slicing, List Comprehension, Strings | PDF",58,[146,150,153,157],{"page_no":8,"url":147,"width":148,"height":149},"https://res-academy.cache.wpscdn.com/upload/kdocshare/2026-11-12/1fc83dcd9fd57c54fae224ea94c7a15e.webp",983,716,{"page_no":14,"url":151,"width":152,"height":149},"https://res-academy.cache.wpscdn.com/upload/kdocshare/2026-11-12/dfde1c034bbd0df7381ae06646517e10.webp",982,{"page_no":81,"url":154,"width":155,"height":156},"https://res-academy.cache.wpscdn.com/upload/kdocshare/2026-11-12/81869e577f26c462bc775e3533b34120.webp",981,715,{"page_no":19,"url":158,"width":159,"height":160},"https://res-academy.cache.wpscdn.com/upload/kdocshare/2026-11-12/0d2c2f70e386a468580a077f79d447f1.webp",980,714,{"list":162,"token":66},[163,169,175,181,187,193,199,205,211,217],{"doc_id":164,"doc_title":165,"file_type":137,"home_preview_img_url":166,"slug":167,"view_count":19,"category_name":30,"create_tm":168},685780,"Tuple in Python - Lecture 2","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000203975/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796110679&Signature=wRbMbmDoXAsV2kQDmDkoURzKpGo%3D","tuple-in-python-lecture-2",1794018232,{"doc_id":170,"doc_title":171,"file_type":137,"home_preview_img_url":172,"slug":173,"view_count":19,"category_name":30,"create_tm":174},697980,"TUPLES AND DICTIONARIES - Introduction to Tuples","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230010146829/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796110679&Signature=f8T64uj8ljFo%2FsKQdgQSJBegJwA%3D","tuples-and-dictionaries-introduction-to-tuples",1794446564,{"doc_id":176,"doc_title":177,"file_type":137,"home_preview_img_url":178,"slug":179,"view_count":24,"category_name":30,"create_tm":180},685778,"1-Tuples","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000203953/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796110679&Signature=NiwHtoweZ1s3Q23g9b7Xiynaq0w%3D","1-tuples",1794018230,{"doc_id":182,"doc_title":183,"file_type":137,"home_preview_img_url":184,"slug":185,"view_count":34,"category_name":30,"create_tm":186},697976,"6.0001 Lecture 5 - Tuples, Lists, Aliasing, Mutability, Cloning","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230010146768/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796110679&Signature=yC0rdk95oRimPsz3BXDnim%2F%2FssY%3D","60001-lecture-5-tuples-lists-aliasing-mutability-cloning",1794446554,{"doc_id":188,"doc_title":189,"file_type":137,"home_preview_img_url":190,"slug":191,"view_count":81,"category_name":30,"create_tm":192},685772,"UNIT-IV - LIST, TUPLES, DICTIONARIES - Problem Solving and Python Programming","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000203776/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796110679&Signature=nyzDsvrsgGCfy5yN9XGNRVQlt%2FU%3D","unit-iv-list-tuples-dictionaries-problem-solving-and-python-programming",1794018219,{"doc_id":194,"doc_title":195,"file_type":137,"home_preview_img_url":196,"slug":197,"view_count":81,"category_name":30,"create_tm":198},685777,"Lists and Tuples - Learn to create and use 1D lists, tuples and 2D lists","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000203949/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796110679&Signature=7a422wvT5cYMjHZ6y3kI2Kp1F%2Fs%3D","lists-and-tuples-learn-to-create-and-use-1d-lists-tuples-and-2d-lists",1794018226,{"doc_id":200,"doc_title":201,"file_type":137,"home_preview_img_url":202,"slug":203,"view_count":29,"category_name":30,"create_tm":204},685629,"Chapter 2: Lists, Arrays and Dictionaries","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229999084017/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796110679&Signature=BHg6WXkMTWnQhVcBbUGtGNHif5Q%3D","chapter-2-lists-arrays-and-dictionaries",1794016092,{"doc_id":206,"doc_title":207,"file_type":137,"home_preview_img_url":208,"slug":209,"view_count":34,"category_name":30,"create_tm":210},670917,"Python 3 Cheat Sheet - concise reference","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229988704071/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796110679&Signature=HC4zC4v3dzghuZgGPq6FixxQOk4%3D","python-3-cheat-sheet-concise-reference",1793572698,{"doc_id":212,"doc_title":213,"file_type":137,"home_preview_img_url":214,"slug":215,"view_count":24,"category_name":30,"create_tm":216},685779,"Topic 6 - Lists, Sets, Tuples, Dictionaries","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000203959/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796110679&Signature=wC7EYXIC0bur3lF8ro0C9tCGU94%3D","topic-6-lists-sets-tuples-dictionaries",1794018231,{"doc_id":218,"doc_title":219,"file_type":137,"home_preview_img_url":220,"slug":221,"view_count":34,"category_name":30,"create_tm":192},685773,"Lists and Tuples - Chapter 7 - Programming Concepts","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000203784/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796110679&Signature=f5%2FVXLuScDad5jzbuy7G2%2F2gPo8%3D","lists-and-tuples-chapter-7-programming-concepts"]