alonzo3569 / Python_Tutorial

My own python notes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Python

My own Python tutorial files

Basic :

  1. help() : help(lst.count)
  2. type() : type(a)
  3. print() :

There are three ways to perform string formatting:

  • %

  • .format().

  • Python 3.6, f-strings.

    1. print ( f ' ' )
      name = 'jose'
      print(f'Hello, his name is {name}') -> Hello, his name is jose
      num = 23.45
      print(f"My 10 character, four decimal number is:{num:10.4f}") -> My 10 character, four decimal number is: 23.4500

    2. print ( " { r : width . precision } ". format ( r = result ) )
      print("The result was {r:3.10}".format(r=result)) -> The result was 0.1287001287
      print("The result was {r:1.3f}".format(r=result)) -> The result was 0.129
      print("The result was {r:10.3f}".format(r=result)) -> The result was 0.129
      Width means the white space before the numbers

    3. "%s" & "%r" , "str()" & "repr()"
      print('I once caught a fish %s.' %'this \tbig') -> I once caught a fish this big.
      print('I once caught a fish %r.' %'this \tbig') -> I once caught a fish 'this \tbig'.
      %r and repr() deliver the string representation of the object, including quotation marks and any escape characters.

    4. For more String Formatting details, check out Ch0 03.ipynb or visit here!

Useful Operators

  1. range() : Generator
range(0,11)

=> Output: range(0, 11)

list(range(0,11,2))

=> Output: [0, 2, 4, 6, 8, 10]

  1. enumerate() : Create a counter(starts from zero) for each object in a list.
for i,letter in enumerate('abcde'):
    print("At index {} the letter is {}".format(i,letter))

=> Output: At index 0 the letter is a\n At index 1 the letter is b\n...

list(enumerate('abcde'))

=> Output: [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e')]

  1. zip() : Generator
mylist1 = [1,2,3,4,5] #mylist1 = [1,2,3,4,5,6,7,8,9,10] also works
mylist2 = ['a','b','c','d','e']
print(list(zip(mylist1,mylist2)))

=> Output: [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e')]

  1. input() : a = input('PLZ input: ') Note : return type 'str'

  2. in :

d = {'mykey':245}

'mykey' in d.keys()

=> Output: True Note: Don't forget '' for mykey

'x' in [1,2,3]

=> Output: False

  1. min() & max() :
mylist = [10,20,30,40,100]
min(mylist)
max(mylist)

=> Output: 10\n 100\n

  1. random library

    • shuffle : Will effect the list passed. Won't return anything.
    from random import shuffle
    shuffle(mylist)
    print(mylist)

    => Output: [40, 10, 100, 30, 20]

    • randint : Return random integer in range [a, b], including both end points.
    from random import randint
    print(randint(0,100))

    => Output: 91

  2. join() : Allows you to join together strings in a list with some connector string.

"--".join(['a','b','c'])

Output => 'a--b--c'

About

My own python notes


Languages

Language:Jupyter Notebook 98.8%Language:Python 1.2%