Learning python (general usage)

For the beginners who have learned a little bit of programming. Even Excel will help.

Lesson 1

Environment Setup

Install the 2 IDEs (Integrated Development Environment) below.

  1. http://www.python.org
    Download Python3.X.X. Do not download Python2.X.X.
    Light weight.
    Use "python IDLE" to test and run the code.
  2. https://www.jetbrains.com/pycharm
    A fancy IDE which has autocomplet and many useful features. There are 2 versions: one is Professional, and the other is Community. Please download the Community.

Online environment

https://repl.it/languages/python3

https://www.codeschool.com/courses/try-python
https://www.learnpython.org/

Solving problems in detail steps with variables, if-else statement, and looping.

Ex. 1: Calculate the average of several numbers.

  1. Let sum be the variable to accumate the numbers.
  2. Let count be the variable to store the count we process so far.
  3. Read each number one by one:
    a) increase the sum by the number
    b) increase count by 1
  4. Finally, the average is sum/count

Ex. 2: Is x a prime:
(assume the x is an integer)

  1. If x is less then 2, it is not a prime.
  2. If x is 2, it is a prime.
  3. If x is divided by any interger from 2 ~ x-1, it is not a prime.
  4. It is a prime.

Ex. 3: Count the number of each letter in an English article.

  1. Let a, b, c, , x, y, z be the variable which are used to stored the count of each letter.
  2. Read each charactor from the article one by one:
    If the charactor is not an English letter, just skip it
    else increase the value of the corresponding variable by 1.

Ex. 4: Make a histogram for the scores of the students in a class.

Ex. 5: Part of "master mind" - guess 1234 for answer 1357

for each digit d in the guessing number(1234 in this example):
    compare the digit in the same position in the answer with d
    if it is the same
        get 1 A
    else
        compare the digit in the different 3 positions in the answer with d

Python code for Ex.2

# assume x is an integer
def is_prime(x):
    if x < 2:
        return False
    if x == 2:
        return True
    for i in range(2,x): # range(a,b) means [a,b)
        if x % i == 0:
            return False
    return True

Small Test

A kid filled all the grids of a Sudoku, but does not know if it is correct.
How do you use your word to discribe the Sudoku rule?

Summary

Think a problem with your own language first. Then try to transform the idea into pseudo code, which uses variables, if-else, and looping. Finally, transform the pseudo code into the target programming language. Don't worry about the last step. You will learn how to code in Python after this lesson.

Lesson 2

Basic input

my_number = int(input())
my_string = input()
my_number = int(input("Please enter a number(1~100): "))

Basic output

print(123)
print("the string")
print("no new line", end="")

Basic objects: integer, float, string, None

a, b = 1, 2
print(a + b)    # 3
c, d = "1",'2'
print(c + d)
print(1 / 3 == 0.3333333333333333) # True
print(1 == 1.00000000000000000001) 
print(1 == 1.000000000001)
print(1 == "1")
print(None == 0)
x = 20
print(x*10)
x = "20"
print(x*10)
print(123 == 123)
print(123 == "123")
print(123 == int("123"))
print(str(123) == "123")

Advanced print (https://pyformat.info/)

x, y = 10, 20
print("{}+{}={}".format(x,y,x+y))
print("{:<4}+{:<4}={:<4}".format(x,y,x+y))

Conditional statement: if-else, if-elif-else

if my_number == ans:
    print("Correct!")
elif my_number > ans:
    print("too large")
else:
    print("too small")

Basic logic operator: <, <=, ==, >=, >

while True:
    x = int(input("input a number 1~100"))
    if 0 <= x <= 100:
        break

Loop statement: for, while, continue, break

for i in range(1,20,2): # range(start, exclude end, each step)
    print(i)
for i in range(10):
    if i == 5:
        continue
    print(i, end="")
while True:
    x = int(input("input a number 1~100"))
    if 0 <= x <= 100:
        break

Small Tests

Q1: Print

*
**
***
****
*****
******
*******
********
*********
**********

Q2: Print

*        *
**      **
***    ***
****  ****
**********

Q3: Let user input an integer N. Output the avg of summation(1~N).

Q4: a, b = 10, 20, please swap the value of a and b

Q5: Print 99 multiplication table with some alignment.

 1 *  1 =  1
 1 *  2 =  2
 1 *  3 =  3
 ...

 4 *  4 = 16
 4 *  5 = 20
 4 *  6 = 24
 4 *  7 = 28
 4 *  8 = 32
 ...
 
 9 *  5 = 45
 9 *  6 = 54
 9 *  7 = 63
 9 *  8 = 72
 9 *  9 = 81

Q6: List the primes <= the user input number N. For example, if user input is 19. List 2,3,5,7,11,13,17,19.

Input the integer as the upper bound: 19
2
3
5
7
11
13
17
19

Q7: Print the first 20 Fabonacci numbers from 0,1,

0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
Select a repo