Thursday, 3 January 2019

Python Crash Course



//multiline text
"""
line1
line2
"""

//format
f"{1+2} {len('name')} {'name'}"

//string
.upper() .lower() .title() ,.strip() .rstrip() .lstrip()  name[start : end] .find('name') .replace('a' , 'b')
a in b a not in b

//number
x = 0b10
print(bin(x))

x = 0x12c
print(hex(x))

x = 1 + 2j
print(x)

print(10/3) 3.3333333333333335
print(10//3) 3
print(10 % 3) 1
print(10**3) 1000

import math
math.floor(1.2)

//buildin function
https://docs.python.org/3/library/functions.html

//math module
https://docs.python.org/3/library/math.html

x = input("x: ")

print(int(x))
print(float(x))
print(bool(x))

#Falsy
# "", 0, [], None (null)

age = 22
if age >= 18:
    print('adult')
elif age >= 13:
    print('teenager')
else:
    print('child')

age = 22
if 18 <= age < 65:
    print('eligible')

age = 22
message = 'eligible' if age >= 18 else 'Not eligible'

for x in "Python":
    print(x)

for x in ['a', 'b', 'c']:
    print(x)

for x in range(0, 10, 2):
    print(x)

print(range(5))
print([1, 2, 3, 4, 5])
print(type(range(5)))

P
y
t
h
o
n
a
b
c
0
2
4
6
8
range(0, 5)
[1, 2, 3, 4, 5]
<class 'range'>

names = ['John', 'Mary']
found = False
for name in names:
    if name.startswith('J'):
        print('found')
        found = True
        break
if not found:
    print('not found')

names = ['John', 'Mary']
for name in names:
    if name.startswith('J'):
        print('found')
        break
else:
    print('not found')

guess = 0
answer = 5
while answer != guess:
    guess = int(input('guess: '))
else:
    pass

guess: 1
guess: 2
guess: 3
guess: 4
guess: 5
PS C:\Users\bob\python1\.vscode>

def increment(number: int, by) -> tuple:
    return (number, number + by)


print(increment(2, by=3))
(2, 5)

def multiply(*list):
    print(list)
    total = 1
    for number in list:
        total *= number
    return total


print(multiply([2, 3, 4, 5]))

([2, 3, 4, 5],)
[2, 3, 4, 5]

def save_user(**user):
    print(user)
    print(user['name'])


save_user(id=1, name='admin')

{'id': 1, 'name': 'admin'}
admin

message = 'a'


def greet():
    global message
    message = 'b'


greet()
print(message)

in debug panel click gear to create launch.json
F5 start debug, F10 debug next line, F11 step into debugging current line

home key move cursor to beginning of line, end key to the end of the line
ctrl + home move cursor to beginning of file, ctrl + end end of the file
select line, hold Alt + arrow to move line up and down
ctrl + / comment out a line

def fizz_buzz(input):
    if (input % 3 == 0) and (input % 5 == 0):
        return 'fizzbuzz'
    if input % 3 == 0:
        return 'fizz'
    if input % 5 == 0:
        return 'buzz'
    return input


print(fizz_buzz(15))


No comments:

Post a Comment