Python Basics

Python Basics

Get Help

  1. Enter the python3 command-line interpretor

  2. Get help for a function, e.g. print(...)

  3. Get help for a class, e.g. class str

  4. Find what variables and methods are available on a class, e.g. class str

  5. Get help for a class method such as rjust(), rjust() is belong to class str

Basic I/O

  1. Print something

  2. Get input from user

String

Concatenation

Concatenate strings with “+”

Formatting

  • Use {} in string literals as a place holder
  • Feed placeholders by passing strings into function format()

String * int

Print a string for 3 times

You will get:

Block

An example block in JavaScript:

However, in Python …

Use indentations instead of curly braces.

Also, there is NO semi-colons.

Number

Try this! Not only in Python!

You will get 5.551115123125783e-17

round()

Exception

Try/Except

List

array in Python

Basics

append()

split()

Call split() on a string will split the string by whitespace (space, tab and newline).

join()

Keyword in

Used to check whether an element is in the list

for Loop in Python

  1. inerating_var in sequence

  2. Iterating by sequence index

    output:

Function

Start with def keyword

Leave a Reply

Your email address will not be published. Required fields are marked *