Skip to main content

Getting started with Python 1

January 22, 2020   

What’s new for me


  • Do not need to declare variables
  • indentation replaces curly braces to divide code blocks

Grammar


Assigning value to multiple variables

x, y, z = 1, 2, "hello"

Define a global variable in local function

def myfunc():
	global x
	x = "fantastic"

Change the value of a global variable inside the function

x = "declan"
def myfunc():
	global x
	x = "declan and jessica"

New data type for me

SampleData Type
x = 1jcomplex
x = ["apple", "banana", "cherry"]list
x = ("apple", "banana", "cherry")tuple
x = range(6)range
x = {"name" : "Bill", "age" : 63}dict
x = {"apple", "banana", "cherry"}set
x = frozenset({"apple", "banana", "cherry"})frozenset
x = Truebool
x = b"Hello"bytes
x = bytearray(5)bytearray
x = memoryview(bytes(5))memoryview

Specifying the data type

x = dataType(value)

Verify the data type

x = 'declan'
type(x)

Random number

import random
x = random.randrange(1,10)

Multiline string (multiline comment)

x = """ declan is a boy,
who is talented in CS,
has more hobbies than that."""

Crop

b = 'declan shares his life'
c = b[2:5]

negative index

c = b[-5:-2]

String method

a = 'Declan and Billy'
len(a)      # length
a.strip()   # remove leading or trailing whitespace characters
a.lower()   # returns lowercase string
a.upper()   # returns uppercase string
a.replace("Billy","jessica") # replace part of the string to another
a.split(" ") #returns "DeclanBilly"
# check if substring exists
x = 'Declan' in a 
x = 'Declan' not in a

String format

Put parameter into {}.

x = "declan's dreaming score is TOEFL{}+ and SAT{}+"
a, b = 115, 1500
print(x.format(a, b))

Also can use {0} to ensure that parameters are placed in correct placeholder.

Must starting with 0 and consecutive

x = "declan's dreaming score is TOEFL{1}+ and SAT{0}+"
a, b = 1500, 115
print(x.format(a, b))

New arithmetic operator

operatornameinstance
**powerx ** y
//floor divisionx // y

Logical operator

and, or, not cannot be written in symbol

Data structure

List

List is an ordered and changeable collection symbolized as []

####### CREATE #######
declanFriends = ["Billy","Cain","Tony","Tom"]

####### VISIT #######
# visit list
print(declanFriends[0])
# negative index
print(declanFriends[-1]) # -1 is the last element in the list
# visit range
print(declanFriends[1:2]) # returns a new list with specified elements
# visit negative index range
print(declanFriends[-4:-1]) # including -4 and excluding -1
# traverse the list
for x in declanFriends:
    print(x)
    ngtr
######## ADD ########
# add a new element to the tail
declanFriends.append("Jessica")
# add a new element to a specified index
declanFriends.insert(1,"Jessica")

######## REMOVE ########
declanFriends.remove("Tom")
declanFriends.pop(1) # remove the last one if no index specified, returning the last element
del declanFriends[1]

####### COPY #######
myFriends = declanFriends
myFriends = declanFriends.copy()
myFriends = list(declanFriends)

####### METHODS #######
myFriends.count("Billy") # returns the number of a specified element
myFriends.index("Billy") # returns the first index of a specified element
myFriends.reverse()
myFriends.sort()

####### OTHERS #######
# the length of the list
print(len(declanFriends))
# clear the list
declanFriends.clear()

list2 = list1 cannot really copy, because list2 will just be a reference to list1. Changing list2 is actually changing list1.

Tuple

Tuple is an orderable and unchangeable collection symbolized as ()

####### CREATE #######
companies = ("Microsoft", "Google", "Apple")
bestCompany = ("Microsoft",) # the ',' is necessary for recognizing variable as tuple

####### VISIT #######
# The same as the above List one

Set

Set is an unordered and no-index collection symbolized as {}

####### CREATE #######
companies = {"Microsoft","Google","Apple"}

####### ADD #######
companies.add("Amazon")
companies.update(("Amazaon","Facebook"))

####### REMOVE #######
companies.remove("Apple") # will raise error if the element is not exist
companies.discard("Apple") # will not raise error if the element is not exist

####### MERGE #######
name = declanFriends.union(companies)

####### OTHERS #######
difference() # returns a collection containing the differences between two or more collections.
difference_update() # delete the same elements from specified set

Dictionary

Dictionary is a unorderable, changeable and indexed collection

Q&A


  1. '' & "", which is recommend?

  2. The different between is and ==?

  3. Return none?

declanFriends = ["a","b","c"]
print(declanFriends.append("d"))
  1. The recommend way to remove elements from list, remove(), del(), pop() or del()?

  2. How to sort list in specific way?

  3. Weird output

thisset = {"apple", "banana", "cherry"}
thisset.update("orange", "mango", "grapes")
print(thisset)
# output: {'o', 's', 'r', 'a', 'e', 'apple', 'm', 'banana', 'g', 'p', 'n', 'cherry'}
  1. What means symmetric_difference() in set method?