Python

Ace your homework & exams now with Quizwiz!

conditional syntax: try and except

'Try' and 'except' mean if one way of running of content after 'try' is not working, run sth after 'except'.

module

module in python is like library in R: a library/feature in a package

How to run a terminal in a specific folder

双击folder,选择new terminal at the folder

format()

A format method can be called to substitute those specifications with corresponding arguments to the format method. For example, age = 25 name = 'Swaroop' print('{0} is {1} years old'.format(name, age)) print('Why is {0} playing with that python?'.format(name)) The output is:

pydoc

Pydoc allows Python programmers to access Python's documentation help files. This is similar to ?<names> in R. For example, type 'pydoc input' in the mac terminal to get help file of input function.

The key differences between Python 2 and Python 3 with examples

http://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html

string formatting operators

https://docs.python.org/2.5/lib/typesseq-strings.html The example is in /learning python the hard way/ex5. I recently identify a topic. Both %s and %r can be used to convert a statement into a string, what's the difference? The reason is: %r displays the raw data of the variable For example, https://stackoverflow.com/questions/6005159/when-to-use-r-instead-of-s-in-python

Python basic operators

https://www.tutorialspoint.com/python/python_basic_operators.htm

input()

input()里面的内容是一个提示性内容,它不是实际的数据。正确的做法是:你使用习题的代码,然后在代码运行的时候,在命令行把你想给出的数据输入进去,这样,你才可能得到希望的数据。但是如果你想要提示他人输入数据,可以写成input(question?)的形式。x = int(input()), which gets the number as a string from input() then converts it to an integer using int().

fast route to run selected part in pycharm

option+shift+e

Local variables

When you declare variables inside a function definition, they are not related in any way to other variables with the same names used outside the function i.e. variable names are local to the function. This is called the scope of the variable. For example, def func(x): print('x is', x) x=2 print('Changed local x to', x) func(x) print('x is still', x) Here, x=2 is a local variable. So the output of func(x) will generate "x is still 50".

while loop

While loop keeps running until the given situations become false or it reaches a break statement. So, without these, the while loop can be an indefinite loop.

return

Why do we need to use return function? Is it really necessary? https://www.codecademy.com/en/forum_questions/51c0e35d7c82caace80008b1

Triple quotes

You can specify multi-line strings using triple quotes - (""" or '''). You can use single quotes and double quotes freely within the triple quotes. For example, '''This is a multi-line string. This is the first line. This is the second line. "What's your name?," I asked. He said "Bond, James Bond." ''' Triple quotes can replace \ and \n in the statement.

true and false- no quotes

You don't need to put quotes around True or False because python recognizes True or False as keywords representing the concept of true or false. If you put quotes around them, python will recognize them as strings and it won't work right.

Escape sequences \ and \n

\ means you specify the single quote. For example, you can specify the string as 'What\'s your name?' \n means you want to start a new line within. For example, "This is the first line\nThis is the second line."

def

Functions are reusable pieces of programs. They allow you to give a name to a block of statements and you can run that block using that name anywhere in your program and any number of times. This is known as calling the function. Functions are defined using the def keyword. This is followed by an identifier name for the function followed by a pair of parentheses which may enclose some names of variables and the line ends with a colon. Next follows the block of statements that are part of this function. For example, def sayHello(): print('Hello World!') sayHello() Output: Hello World!

A bunch of escape sequences

Here are the examples, https://linuxconfig.org/list-of-python-escape-sequence-characters-with-examples

number type conversion

How to convert integers into floating numbers or vice versa?

string conversions

How to convert string into integers?

Global statement

If you want to assign a value to a name defined at the top level of the program (i.e. not inside any kind of scope such as functions or classes), then you have to tell Python that the name is not local, but it is global. We do this using the global statement. For example, x = 50 def func(): global x print('x is', x) x=2 print('Changed global x to', x) func() print('Value of x is', x) Here, we get the value of x to be 2 because we use the global statement above.

semicolon(;)

If you want to specify more than one logical line on a single physical line, then you have to explicitly specify this using a semicolon (;) which indicates the end of a logical line/statement. ie: i = 5; print(i); You will get an output of 5. However, I strongly recommend that you stick to writing a single logical line in a single physical line only. Use more than one physical line for a single logical line only if the logical line is really long. The idea is to avoid the semicolon as much as possible since it leads to more readable code. In fact, I have never used or even seen a semicolon in a Python program.

Unicode, UTF-8, bytes

In python 3, all strings are unicode(ASCII or UTF-8, they are interchangable). encode() is a way to change unicode into bytes, the method is through UTF-8, so the computer can connect with outside network.

Indentation

Indentation is the whitespace at the beginning of the line.(每种statements前面空相同数量的空格) How to indent: Do not use a mixture of tabs and spaces for the indentation as it does not work across different platforms properly. I strongly recommend that you use a single tab or four spaces for each indentation level. Choose either of these two indentation styles. More importantly, choose one and use it consistently i.e. use that indentation style only.

variables in Python

Numerical variables: integer variables and float variables String variables boolean variables: True or False None variable: none

default argument values

Only those parameters which are at the end of the parameter list can be given default argument values i.e. you cannot have a parameter with a default argument value before a parameter without a default argument value in the order of parameters declared in the function parameter list. This is because the values are assigned to the parameters by position. For example, def func(a, b=5) is valid, but def func(a=5, b) is not valid.

Function parameters

Parameters are specified within the pair of parentheses in the function definition, separated by commas. For example, printMax(a, b) a and b are function parameters. The values you supply in the function are called arguments. For example, def printMax(a,b) x=5 y=7 printMax(x,y)

modulo operator (%)

The % (modulo) operator yields the remainder from the division of the first argument by the second. The numeric arguments are first converted to a common type. A zero right argument raises the ZeroDivisionError exception. The arguments may be floating point numbers, e.g., 3.14%0.7 equals 0.34 (since 3.14 equals 4*0.7 + 0.34.) The modulo operator always yields a result with the same sign as its second operand (or zero); the absolute value of the result is strictly smaller than the absolute value of the second operand [2]. The % (modulo) operator yields the remainder from the division of the first argument by the second. The numeric arguments are first converted to a common type. A zero right argument raises the ZeroDivisionError exception. The arguments may be floating point numbers, e.g., 3.14%0.7 equals 0.34 (since 3.14 equals 4*0.7 + 0.34.) The modulo operator always yields a result with the same sign as its second operand (or zero); the absolute value of the result is strictly smaller than the absolute value of the second operand [2]. Taken from http://docs.python.org/reference/expressions.html Example 1: 6%2 evaluates to 0 because there's no remainder if 6 is divided by 2 ( 3 times ). Example 2: 7%2 evaluates to 1 because there's a remainder of 1 when 7 is divided by 2 ( 3 times ).


Related study sets

Nervous and Reproductive systems review

View Set

Exam 1 - PT 625 Professional Development I

View Set

Oceanography Exam - Questions Chapter 1

View Set

Lateralisation and Split-brain Research

View Set

Introduction to Environmental Science

View Set

Week 4- Overview of Inferential Statistics

View Set

Including Students with Special Needs: 1

View Set