June+3rd+2016+by+Arthur

June 3rd, 2016 by Arthur Chen

** Learning outcomes -- Edited by Arthur Chen on June 3 ** rd
Today we first reviewed yesterday's homework section --- chapter two of the book //How to think like a computer scientist.// In this section, we learnt about the term 'variables', its categories, or class, and its properties. You can [|click me] to see that chapter.

There are several classes that we should pay attention to: change base ||= int int( number, base ) ||= 6 int(11,2) ||= same as in maths int(11,2) gives 3 || a[1] gives 't' ||
 * = **name** ||= **abbreviation** ||= **meaning** ||= **converter symble / style** ||= **example** ||= **operation** ||
 * = integer ||= int ||= integers
 * = float ||= float ||= decimals ||= float ||= 2.333 ||= 3.0-3.4 gives -0.39999 ||
 * = string ||= str ||= a sequence of characters ||= str / ' ' ||= a = 'string' ||= 'a' + 'b' = 'ab' ≠ 'b' + 'a'
 * = boolean ||= bool ||= True or False ||= bool ||= True ||= True or False ||
 * = dictionary ||= dict ||= / ||= { } ||= {cool:'me',fun:'CS'} ||= cool gives 'me' ||
 * = tuple ||= tuple ||= / ||= tuple / ||= a = (1,2,3,4,5) ||= a[1] gives 2 ||
 * = list ||= list ||= a list of elements ||= list / [ ] ||= a = [1,2,3,4,5] ||= a[1] gives 2 ||

ATTENTION:
 * 1) Python is Case Sensitive, so __True__ is very different from __true__.
 * 2) Python does NOT need declaration, data type can be changed along the process.

Then we talked about statements, which are instructions that the Python interpreter can execute. The statements are always on the right side of assignment expressions. Note that the code: code format="python" a = 10 code means //a// is given the value 10, so it reads: "a is assigned 10." Also note that when we are naming a variable, we should follow several rules: code format="python" code Then we studied how to use functions like __//while, if//__ and //__for__//: code format="python" Class=6 while 1:             #also can be written as while True: if Class >= 10: break print(Class) Class = Class + 2 code The result is 6, 8. code format="python" code The function //__while__// works like this : while __//a condition to stop//__: __tab__ __process__
 * 1) We should pay attention to the capital letters, as said before, Python is case sensitive.
 * 2) We CANNOT begin the name with numbers, it will make a syntax error.
 * 3) We should NOT begin the name with _ as they usually mean something else.
 * 4) We should avoid using Python Keywords as follow:

The function __//if//__ works like this: if __//a condition to begin//__: //__tab__ __process__// elif __//a condition to begin//__: #not necessary //__tab__ __process__// else: #not necessary //__tab__ __process__// code format="python" code The function //__for__// works like this: code format="python" code for __//a variable//__ in __//a range//__: //__tab__ __process(including changing the variable)__//

ATTENTION:


 * 1) The indentations ( __tab, or four spaces__ ) in these codes are very important!
 * 2) Don't forget the colons, or there will be syntax error!

The function //__import__// is often used at the beginning of the code, it provides packages of functions that can be used later on. For example: code format="python" code code format="python" >>> import math >>> math.pi 3.141592653589793 >>> math.cos(math.pi) -1.0 code Here, math.pi is a setted variable carrying the value 3.141592653589793, so we can simply use it in our code.

By the way, we were introduced to a useful function: len. It can calculate the length of a string. For example: code format="python" code code format="python" >>> a='qwerty' >>> len(a) 6 code

After this, we were trying to use more operators other than +, -, *, /, and **. These are // //and %.// a // b //means to count how many// b//s are in// a//. For example 11// // 3 gives 3. % means to calculate the remainder after the division, for example 11%3 gives 2. These to operators are useful for converting millions of seconds into hous, minutes and remaining seconds.

At the last of chapter 2, we learnt to let our programs receive inputs by using the function __input.__ We should always keep in mind that what we type in is always treated as a string. So if we want to use it as an integer, we should first change its datatype by applying the converter int. This can be done in the same line as input. Also, we can enter some instructions in the parentheses after input to tell the typer what kind of data we want him to give. Here's an example: code format="python" code code format="python" >>> a = int(input('Please enter an integer ')) 'Please enter an integer' 3 >>> a 3 >>> type(a)  code Combining what we'd learnt, we can then solve the time conversion problem: code format="python" t=int(input('seconds= ')) print(t//(3600*24),' days',(t%(3600*24))//3600,' hours ',(t%3600)//60,'minutes and ',t%60,'seconds')

code At the end of the class, we began chapter 3, turtles. The 'turtle' is a package that is needed to be imported as mentioned before. There are so many fancy features in this package and thus we can create fantastic drawings. code format="python" code The basic code is: code format="python" import turtle wn = turtle.screen alex = turtle.Turtle alex.forward(100) alex.left(90) code We can explore more when we are doing our homework! Resource link [|https://docs.python.org/3/library/turtle.html#compound-shapes]

Have fun!