Programming with Python: An Easy to Understand Beginners Guide to Coding with Python by Marc Stanford

Programming with Python: An Easy to Understand Beginners Guide to Coding with Python by Marc Stanford

Author:Marc Stanford [Stanford, Marc]
Language: eng
Format: mobi, pdf
Publisher: Independently published
Published: 2019-09-25T00:00:00+00:00


➢ Lists, Tuples, Sets, JSON

Python has a great built-in list type named "list". List literals are written within square brackets [ ]. Lists work similarly to strings -- use the len() function and square brackets [ ] to access data, with the first element at index 0.

# List

>>> fruits = [“apple”, “banana”, “cherry”]

# Add new item into array

>>> fruits.append(“orange”) #[‘apple’, ‘banana’, ‘cherry’, ‘orange’]

# Pop last item from the aray

>>> fruits.pop() #[‘apple’, ‘banana’, ‘cherry’]

# Remove any element from the array

>>> fruits.remove(‘banana’)

# [‘apple’,’cherry’]

>>> print(fruits.index(‘cherry’))

# 1

A “tuple” is a sequence of immutable Python objects. “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. Creating a “tuple” is as simple as putting different comma-separated values. Optionally you can also put these comma-separated values between parentheses.

# Tuple

# A tuple is a collection which is ordered and unchangeable.

# In Python tuples are written with round brackets.

>>> thistuple = (“cherry”, “orange”, “kiwi”, “melon”, “mango”)

>>> print(thistuple[-4:-1])

# (‘orange’, ‘kiwi’, ‘melon’)

# Join 2 tuples

>>> tuple_number = (1, 2, 3)

>>> print(thistuple[:3] + tuple_number)

# (‘apple’, ‘banana’, ‘cherry’, 1, 2, 3)

>>> print(thistuple.index(‘cherry’))

# 2

A “set” is an unordered collection of items. Every element is unique (no duplicates) and must be immutable (which cannot be changed). However, the “set” itself is mutable. We can add or remove items from it. “Sets” can be used to perform mathematical “set” operations like union, intersection, symmetric difference, etc.

# Set

# A set is a collection which is unordered and unindexed

# In Python sets are written in curly brackets

>>> thisset = {“apple”, “banana”, “cherry”}

# Add new item to this set

>>> thisset.add(“orange”)

# {‘orange’, ‘cherry’, ‘banana’, ‘apple‘}

>>> thisset.update([“orange”, “mango”, “grapes”])

# {‘mango’, ‘orange’, ‘cherry’, ‘banana‘, ‘grapes’,‘apple‘}

>>> thisset.remove(“mango”)

# {‘grapes’, ‘cherry’, ‘apple‘, ‘orange’,‘banana‘}

>>> list1 = [11,12,22,23,11,22,44,55,66,23]

>>> print(set(list1))

# {66,11,12,44,55,22,23}

>>> set2 = {1,3,4,5}

>>> print(thisset.union(set2))

# {1, ‘apple’, ‘banana’, 3, 4, ‘orange’, ‘grapes’, ‘cherry’, 5}

“JSON” (Java Script Object Notation) is a popular data format used for representing structured data. It's common to transmit and receive data between a server and web application in “JSON” format. In Python, “JSON” exists as a string.

# JSON

>>> import json

# some JSON:

>>> x = ‘{“name”, “John”, “age”:30, “city”:”New York”}’

# parse x

>>> y = json.loads(x)

# the result is a Python dictionary:

>>> print(y[“age”])

# 30

➢ Datetime



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.