Posts

Showing posts from July, 2017

Python 3 Tuples

The tuple data type is almost identical to the list data type, except in two ways. 1) Tuples are typed with parenthesis ( and ) instead of square brackets [ and ] 2) Tuples are immutable, whereas lists are mutable. Benefit of using tuples: You can use tuples to convey to anyone reading your code that you don't intend for that sequence of values to change. If you need an ordered sequence of values that never changes, use a tuple. A second benefit of using tuples instead of lists is that , because they are immutable and their contents don't change. Python can implement some optimizations that make code using tuples slightly faster than code using lists. E.g. fruits = ('Banana', 'Apple', 'Carrot', 'Strawberry') If you have only one value in your tuple, you can indicate this by placing a trailing comma after the value inside the parenthesis. Otherwise, Python will think you've just typed a value inside regular parenthesis. E.g. f

Python 3 Lists

A list is a value that contains multiple values in an ordered sequence. Example of list : fruits = ['Banana', 'Apple', 'Carrot', 'Strawberry'] A list begins with an opening bracket and ends with a closing bracket. Values inside the list are called items, and they are separated by commas. Getting Individual Values in a List with Indexes: Consider the above list for example, the python code fruits[0] would evaluate to 'Banana' and fruits[1] would evaluate to 'Apple' and so on. The integer inside the square bracket that follows the list is called an index. The first value in the list is at index 0, the second value is at index 1, the third value is at index 2 and so on. Indexes can only be integer values not floats. For example, fruits[1.0] will give error. Lists can also contain other list values. The values in these lists of lists can be accessed using multiple indexes, for example: fruits = [['Banana', 'App

Python 3 Variables

Variables are nothing but reserved memory locations to store value. This means when you create a variable you reserve some space in memory. Python is completely object oriented and not "statically typed". You no need to declare variables before using them, or declare their type. Every variable in python is an object. Every value in python has data type. Different data types in python are: Numbers String List Tuple Dictionary Python Numbers: Number data types store numeric values. Number objects are created when you assign a value to them. For example: var1 = 32 var2 = 42 Python supports two types of numbers: Float Integers To define a floating point number, you may use the following: pi = 3.14 print(pi) You can redeclare variable even after you have declared once: f = 0 print(f) f = "cat" print(f) Concatenate Variables: You can concatenate variables of same type. f = "dog" + "cat" print(f) For