Basics Of Tuples | Development | Online Course With Appy Pie Academy

Basics of Tuples

Lesson Details:
June 29, 2020


I: Introduction

The purpose of this paper is to show different features of python programming. Python is a high level programming language that was developed in the late 1980’s by Guido van Rossum. It is a general-purpose, high-level language that has been used for a variety of applications. In this paper, I will go through some basic concepts of python programming.

II: Body

In the first section, I will discuss how to do basic programming in python. In other sections, I will discuss different features of tuples and strings.

Basic programming in Python

The first step is to open the terminal. This will start the Python interpreter. Then, type “print(‘Hello World!’)” at the prompt. The next line should be “>>>”. This means that everything you enter after typing it, will be interpreted as Python code.

>>> print('Hello World!') Hello World! >>> 1 + 1 2 >>> exit() $

After you have entered the programming code, press “Ctrl + d”. This is the same thing as pressing the “enter” key on your keyboard. The last line should say “$”. This means that you are back at the bash prompt.

A tuple is an immutable sequence of values. A tuple can hold any number of values of any data type. You can also change the values inside of a tuple, but you cannot change the values. Tuples are similar to lists in Python, but they cannot be changed once created (See (1))). Tuples are generally used when you want to return multiple values from a function or method without returning a list or any other collection type. You can create a tuple with one variable or multiple variables separated by commas. For example, if you wanted to create a tuple with two integers, just put the integers between parentheses, separated by commas. Similar to lists, if you want to have one of the variables be a float, type the value just like you would type it outside of a tuple. If you want to have an empty tuple, put the variables in parentheses without anything else between them. One difference between tuples and lists is that if you want to access each of the variables in a tuple, use the indexing operator ([]) instead of using indexes. The following are examples of how tuples are declared, shown with their values printed out:

>>> t = (1, 2) >>> t[0] 1 >>> t[1] 2 >>> t[0] = 5 Traceback (most recent call last): File "", line 1, in TypeError: 'tuple' object does not support item assignment >>> t 5 >>> t = () >>> t () >>> t[0] = 5 Traceback (most recent call last): File "", line 1, in TypeError: 'tuple' object does not support item assignment >>> t () >>> t[0] = 5 Traceback (most recent call last): File "", line 1, in TypeError: 'tuple' object does not support item assignment >>>

A string is a series of characters and it always holds characters and nothing else. To create a string, just enclose the characters inside double quotes. Similar to tuples, if you want to have one of the characters be a float, type the value just like you would type it outside of a string. If you want to have an empty string, put the characters inside double quotes without anything else between them. Strings can also contain variables; this allows you to reference those variables within the string. For example:

>>> var_one = 0 >>> var_two = 100 >>> my_var = "There are %d%s and %d%s" % (var_one, var_two, var_two) >>> my_var 'There are 0%s and 100%s' >>> my_var[0] 'There' >>> my_var[1] 'are' >>> my_var[2] '100' >>> my_var[3] 's' >>> my_var[4] Traceback (most recent call last): File "", line 1, in IndexError: string index out of range >>> my_var[-1] 's' >>> my_var[len(my_var)] 's' >>> my_var[:] 'There are 0%s and 100%s' >>> my_var[:] = "Merry Christmas!" >>> my_var 'There are 0%s and 100%s Merry Christmas!' >>> exit() $

III: Conclusion

In conclusion, I have explained how tuples and strings work in Python programming. In order to do basic programming in Python, you need to open a terminal and start python interpreter. If you want to change the value inside a tuple or a string you need to use indexing operator ([]) instead of using indexes.

loader
Course content