Skip to content
This repository has been archived by the owner on Dec 4, 2021. It is now read-only.

Extreme simplified notes for python

WZYDominic edited this page Sep 4, 2021 · 13 revisions

IMAGE ALT TEXT HERE
To get Table of Contents, expand Pages then expand Editing Extreme simplified notes for python

Defining

x=3 #define x as an integer and assign 3 to x(value of x can be changed later)
setname = ['item1', 'item2', 'item3'] #define 'setname' as a set

Printing Stuff

print('hello')

hello

Type and class declaration

count = 1
print(count)
type(count)//gives the type of the variable (e.g int, 

1

<class 'int'>

Retrieving random function

import random
x = random.randint(5, 10)
print(x)

8 //gives a number between 5 and 10 inclusive

Create own function

def print_test()://defining a function
    print('test')
print_test()
print(' ')//create a space(refer to [1])

def print2_test():
    print_test()//initiating a function within another function
    print_test()
print2_test()

test

[1]

test

test

Function with argument

def test_print(x):
    print(x)
y=input('anything')
test_print(y)

anythingwzyd123

wzyd123

Retrieving math function

import math //retrieve math 

Conditional

x = int(input("number"))
if x > 0:
    print('x is positive')
elif x == 0:
    print('x is zero')
else:
    print('x is negative')

number1

x is positive

TRY and EXCEPT

x = input("number")
try:
    y = float(x)
    z = (y-1)+1
    print(z)
except:
    print('enter a number')

numberz

enter a number

Taking a input

Ttmp = input("key in temperature:")
print("temperature: ", temp)

key in temperature: 74

temperature: 74

"while" statement##(use True for infinite loop)(use False to eliminate loop)

n=5
while n>0:
    print(n)
    n = n-1
print('Blastoff!')

5

4

3

2

1

Blastoff!

Finishing and continuing iteration using "continue" and break

while True://infinite loop
    line = input('> ')
    if line[0] == '#'://line[0] is the first character of the string
        continue
    if line == 'done'://if input is 'done', infinite loop stops
        break
    print(line)
print('Done!')

Definite loop using "for" loop

name = ['friend1', 'friend2', 'friend3']//define name as set and assign friend(1-3) in the set
for friend in name://for loops continue until all items in set have been "used"
    print('Hello', friend)
print('Done!')

Hello friend1

Hello friend2

Hello friend3

Done!

Counting items in a set

count = 0
for itervar in [3, 41, 12, 9, 74, 15]:
    count = count + 1
print('Count: ', count)

Count: 6