Git Product home page Git Product logo

u4_lesson_python_tuples's Introduction

Python Tuples

Overview

In this lesson, we'll learn about Python tuples and their proper use cases.

Objectives

  • Learn proper use of tuple data types
  • How to work with tuples
  • Learn the limitations of tuples

Getting Started

  • Fork and clone this repository

What Are Tuples

A tuple is a collection of objects which ordered and immutable. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.

Unfortunately javascript does not have a tuple equivalent as of this writing.

A tuple is declared by using () or the tuple constructor, example:

tuppy = ()

new_tup = tuple()

Here's an example of a tuple:

my_tup = (1,3,4)

Tuples can have a mixture of data types:

mixed_tup = (1,'String',False, None)

A tuple cannot be modified in any way, it's best used for data that you do not want to be modified. For example if you look at the sequelize insert queries, you'll see that they are using something similar to tuples:

INSERT INTO "task" ("id","created_at","updated_at","name","status") VALUES (DEFAULT,'2018-10-23 13:58:05.380 +00:00','2018-10-23 13:58:05.380 +00:00','First task',1) RETURNING *

Accessing Tuple Values

Accessing values in tuples is performed the same way we access items in a list:

my_tup = (1,2,3,4)
print(my_tup[1]) # Returns 2
print(my_tup[1:4]) # Returns 2,3

We can also iterate through tuples:

my_tup = ('Hello', 'World')

for word in my_tup:
    print(word) # Prinst Hello, World

Tuple Methods

Python has the ability to read tuple values:

  • Finding Tuples Max Value:

      tup = (1,2,3)
      max(tup) # Returns 3
  • Finding Tuples Min Value:

      tup = (1,34,21)
      min(tup) # returns 1
  • Converting Lists Into Tuples:

      my_list = [1,2,3,4]
      new_tup = tuple(my_list)
  • Converting Tuples Into Lists:

      my_tup = (1,2,3)
      my_list = list(my_tup)
  • Deleting Tuples:

      my_tup = (1,2,3,4)
      del my_tup
      print(my_tup) # my_tup is undefined

You Do

Follow the directions in main.py, test your code with python3 tuple_test.py.

Recap

In this lesson we learned about tuples in Python. A few key things to note about tuples:

  • Tuples are immutable, meaning that they cannot be modified or updated
  • Tuples are typically used for data that you want to be secure when passed on to another function or operation

Resources

u4_lesson_python_tuples's People

Contributors

anpato avatar ahonore42 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.