您的位置:首页 > 编程语言 > Python开发

A document for my learning journey in Python - Day 1&2

2017-07-06 08:37 471 查看
Reference: https://www.codecademy.com/learn/python

statement: all the following ideas/source codes are the re-creation based on the "codecademy"

1. Indentation is important, e.g.

def spam():

    eggs = 12

    return eggs

        

print spam()

//result --> 12

2. Math operation, read more--> https://en.wikibooks.org/wiki/Python_Programming/Basic_Math



a) +

b) -

c) *

d) /

e) ** exponential

d) % modulo, e.g. 3%2 --> 1

3. Escaping character

'This isn\'t flying, this is falling with style!' # To make sure the ( isn't ) is working ( isn\'t )

4. Access by Index

e.g. fifth_letter = "MONTY"[4] #result --> Y

5. String methods

a) len() #e.g. len(test)

b) lower() #e.g. test.lower()

c) upper()

d) str()

#e.g. str(3.14), now 3.14 is string type --> "3.14"

e) string is target --> string equality checking, take note the target cannot have a space in between.

6. String Formatting --> With %

e.g. string_1 =  raw_input("What is your name?")  #getting the input from user

e.g. string_2 = "place"

e.g. print "Let's not go to %s. 'Tis a silly %s." % (string_1, string_2)

e.g. result --> Let's not go to Camelot. 'Tis a silly place.

7. Date and Time Library

a) Import the library --> from datetime import datetime

b) Datetime methods --> datetime.now() # now.year, now.month, now.day, now.hour, now.minute, now.second

8. Comparators 

a) Equal to (==)

b) Not equal to (!=)

c) Less than (<)

d) Less than or equal to (<=)

e) Greater than (>)

f) Greater than or equal to (>=)

9. And (and) and Or (or) operation



**Take note: not is evaluated first, and is evaluated next, or is evaluated last.

a) and --> only two Ture yields True, otherwise False e.g. 00, 01, 10 false, 11 True.

b) or --> only two False yields False, otherwise True e.g. 00 True, 01 10, 11 False.

c) not --> invert

10. Conditional Statement Syntax

a) if ______ : # take note, the semicolon is needed --> : 

b) example:

def greater_less_equal_5(answer):

if answer = 5:

  return 1

    elif answer = "test":          

        return -1

    else:

      return 0

11. function

a) keyword --> def test():

e.g. def test ( p1, p2):

e.g. test(1,2)

b)  triple quotes --> """ trip;e quotes server the purpose of explaning the funciton""""

12. Generic Import

a) import math --> math.mathmethod() --> allow user to use the methods/functions within the math module

--> import a specific function/method from a module: from math import sqrt

--> import all from a module: from math import * --> this is not so good because it will associate with many unused variables which is not safe

--> get access to all the methods/functions from a module --> everything = dir(math)

<
926b
p>

13 Analytic method read more: https://community.modeanalytics.com/python/tutorial/python-methods-functions-and-libraries/

a) max()

b) min()

c) abs()

d) type() --> return the type of the varibale within the bracket

-e.g. type(test) == int #checking the type

14. List, using square bracket to indicate it  -- name [  ]

a) does not have a fixed length 

b) use append to add in parameters --> e.g. list.append(new_item)

c) To access certain parts of the list variables -->  list[0:2] 

-list[0:2] is using 1st and 2nd items (position 0 and 1) and the list is counting from 0,

-take note, the "2" is larger than the largest number you want to access.

d)List method, list.sort()

e) List association, e.g.  list['test1':101, "test2",102]

-This allowed us to access the ID via the 'name'

-This does now allowed us to access the 'name' via the ID

f) List quick method/ dot menthod,

-del list[item1]

g) delete/remove an item

-list.remove[item_name]

15. Dictionary, using curly brackets to indicate it -- name {  }

a) Format: d={ 'test':101, "test2":102, ‘key’, 'keyvalue' }

b) Adding element into the Dictionarys -- adding a key to the dictionary, the key can then used to contain List and other types.

-- name ['key'] # adding a new item to the dictionay

-- name ['key'] = keyvalue  # keyvalue can be number or string

--e.g. name ['key'] = ['1', '2', '3' ] # adding a list to the key

-- the line at above is able to use to resign the keyvalue to the key

--name[keyvalue] = something #this will reassign the content to the keyvalue

c) How to access an item in the dictionary.

-- e.g. name['key'].sort()

-- e.g. name['key'].remove('item')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: