绵绵瓜瓞网

Python Language Megadoc

Python Language Megadoc

  • Python is not compiled (run directly)
  • Python is statically typed (the types of the variables cannot change)
  • Python is not strongly typed(do not declare types)
  • Python does not use curly braces (use tabs and indentations)
  • Boolean values are now True and False
  • Logic operator are now and or

Strings

  • Use single quotesdouble quotes

  • """triple quotes to comment blocks of code

Access length

  • len(s)

Access charAt(index)

  • s[index]
  • s[-1] -- counts from the end, so last char of the string

Access substring

  • s[1:4] -- from 1 to 3, excluding the end
  • s[1:4:2] -- from 1 to 3, but stride 2, i.e. skipping every other character
  • s[2:] -- from 2 to end
  • s[2:10000] -- from 2 to end
  • s[:2] -- from start to 1, excluding the end
  • s[:] / s[::] -- copy the entire string
  • s[::-1] -- copy and reverse the string

Splitting a string

  • s.split(' ') -- split by space (only one space)

Remove spaces

  • s.strip() -- removes all spaces (front and back) from a string

Compare strings

  • s == 'abc' -- compare values

Converting to string

  • str(123) -- '123'

For loop

for ... in ... :

        print();

Specifying range

  • range(5) -- 0 to 4, excluding the end
  • range(0,5) -- -- 0 to 4, excluding the end

Iterate through elements

  • forx inarr

Iterate through dictionary

  • forkey, vallue indict.items()

未经允许不得转载:绵绵瓜瓞网 » Python Language Megadoc