Last active
October 28, 2017 17:34
-
-
Save jtjoo/5f00f98b36e760a0ffb90ad2201a3bd1 to your computer and use it in GitHub Desktop.
Python Study
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# if | |
if True: | |
print(" It's condition is True ") | |
# if-else | |
if False: | |
print(" It's condition is True ") | |
else: | |
print(" It's condition is False ") | |
# if-elseif-else | |
x = 10 | |
if x<0: | |
print(" x is smaller than 0 ") | |
elif x==10: | |
print(" x is 10 ") | |
else: | |
print(" x is nothing else") | |
# and, or | |
if x > 0 and x < 20: | |
print(" x is larger than 0 and smaller than 20 ") | |
if x > 0 or x < 10: | |
print(" x is not smaller than 10, but x is larger than 0, So it is true statement. ") | |
# double if | |
if x > 0: | |
print("x is larger than 0!") | |
if x < 10: | |
print("x < 10 is true!") | |
else: | |
print("x < 10 is not true!") | |
print("after double if") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
birthyear = 1983 | |
running = True | |
# while | |
while running: | |
guess = int(input("Guess Jintae's birth year : ")) | |
if guess == birthyear: | |
running = False | |
elif guess > birthyear: | |
print("Thank you... but it's less than mine.") | |
elif guess < birthyear: | |
print("I'm not old like that.") | |
else: | |
print("That's right!") | |
print("done") | |
# for .. in | |
for x in range(1,10): | |
print("Count {}".format(x)) | |
if x == 5: | |
break | |
# see what hanppens when break encountered. | |
else: | |
print("for loop is over!") | |
# when break encountered, this doesn't work. | |
# continue, break | |
while True: | |
keyword = input("input something : ") | |
if keyword == "exit" or keyword == "quit": | |
break | |
if len(keyword) < 4: | |
print("Too small!") | |
continue | |
print("done") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# list | |
wishlist = [ "PS4 Pro", "Candy Red Colored Mustang Guitar", "Old BMW Car" ] | |
print( len(wishlist), "items in 'wishlist' " ) | |
# show list | |
def printItems(): | |
idx = 0 | |
print( "Items are : ", end=' ' ) | |
for item in wishlist: | |
idx = idx + 1 | |
if idx < len(wishlist): | |
print( item, end=', ' ) | |
else: | |
print( item ) | |
printItems() | |
# append | |
wishlist.append("MacBook Pro New Model w/TouchBar") | |
print( "Also I want to have :", wishlist[len(wishlist)-1] ) | |
print( "\nNow, All the items in wishlist are like above" ) | |
print( "-------------------" ) | |
printItems() | |
# sort | |
print("\nAnd, I will sort my items, default is by alphabet asc") | |
print( "-------------------" ) | |
wishlist.sort() | |
printItems() | |
# sort reverse | |
print("\nReversed Sort") | |
print( "-------------------" ) | |
wishlist.sort(reverse=True) | |
printItems() | |
# get item by index | |
print("\nFirst Item is :", wishlist[0]) | |
print("Last Item is :", wishlist[len(wishlist)-1]) | |
# delete item | |
print("I don't need Last item right now. I'll need it later...") | |
del wishlist[ len(wishlist)-1 ] # delete last item, Candy Red Mustang Guitar (because it sorted) | |
print("Now, ", end=' ') | |
printItems() | |
# define search and delete function | |
def searchAndDelete(keyword): | |
idx = 0 | |
foundItem = 0 | |
for item in wishlist: | |
if item.find(keyword) > -1: | |
foundItem = foundItem + 1 | |
print( "found \"", item, "\" and I will delete it..." ) | |
del wishlist[idx] | |
idx = idx + 1 | |
return foundItem | |
# I referenced 'find' method in link above : | |
# https://www.tutorialspoint.com/python/string_find.htm | |
# search and delete | |
print( "find 'BMW' in wishlist and delete..." ) | |
print( searchAndDelete('BMW'), "items found and deleted" ) | |
print( "Now my wishlist is " ) | |
printItems() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# regular function | |
def say_hello(): | |
print("Hello World") | |
# local variable | |
x = 10 | |
def check_x(x): | |
print("x is ", x) | |
x = 1 | |
print("now x is ", x) | |
check_x(x) | |
print('x is still', x) | |
# use global variable | |
def use_global(): | |
global x | |
x = 5 | |
print("now x is", x) | |
use_global() | |
print('x is now', x) | |
# return value | |
def func_add(x, y): | |
return x+y | |
print("1 + 5 = ", func_add(1, 5)) | |
# default value | |
def use_default(x=10): | |
if x==10: | |
print("x is default value") | |
else: | |
print("x is not default value") | |
use_default() | |
use_default(5) | |
# keyword argument | |
def keyarg(a=0, b=1, c=2): | |
print("a :",a," b :",b," c :",c) | |
keyarg() | |
keyarg(a=10, c=10) | |
keyarg(c=50, a=20) | |
# VarArgs Parameters | |
def VarArgsFunc(*numbers, **emails): | |
'''It takes numbers and print sum of numbers | |
And takes dictionary of names and emails and print it.''' | |
# print sum of numbers | |
sum_of_numbers = 0 | |
for x in numbers: | |
sum_of_numbers += x | |
print("Sum of numbers is :", sum_of_numbers) | |
# print emails | |
for name, email in emails.items(): | |
print(name, "'s email address is :",email) | |
VarArgsFunc(1,2,3,4,5,6,7,8,9,10,Jintae="jtjoo@me.com",Johnny="john@python.com",Ian="ianjoo@python3.com") | |
print(VarArgsFunc.__doc__) | |
# print function documents and description about parameters with less | |
help(VarArgsFunc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Name search | |
file = open("names") | |
print("\n\nName Search Result") | |
print("----------------------") | |
for line in file: | |
if line.startswith("John"): | |
print(line) | |
# Email Search | |
file = open("names") | |
print("\n\nEmail Search Result") | |
print("----------------------") | |
for line in file: | |
# if line.startswith("John"): | |
name, email = line.split(",") | |
if name == "John Joo": | |
print(email) | |
################################### | |
# Contents of file "names" | |
################################### | |
# | |
# John Denver,johndenver@gmail.com | |
# Jintae Joo,jintaejoo@gmail.com | |
# Ian Curtis,iancurtis@gmail.com | |
# Ian Johnnas,ianjohnnas@gmail.com | |
# Johnathan Davis,johnathandavis@gmail.com | |
# John Joo,johnjoo@gmail.com | |
# John Lee Joo,johnjoo@gmail.com | |
# John Hammerstein,johnhammerstein@gmail.com | |
# Thomas Lee,thomaslee@gmail.com | |
# Siyeon Lee,siyeonlee@gmail.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Quotes (single, double) | |
print('Hello') | |
print("Hello") | |
# Triple Quotes | |
print(''' | |
Hi, This is Jintae | |
I am learning python | |
to rebuild my old event sources. | |
''') | |
# Calculate Strings | |
print('Hello' + " World") | |
print("Hello 'World'") | |
print('Hello' * 3) | |
# Print by Position | |
print('Hello'[0]) | |
print('Hello'[1]) | |
print('Hello'[4]) | |
print("Hello"[3]) | |
print("Hello"[0]) | |
# Functions in String Object | |
print("hello world".capitalize()) | |
print("hello world".upper()) | |
print("HELLO WORLD".lower()) | |
print("hello world".__len__()) | |
print(len("hello world")) | |
print('hello world'.replace( 'world', 'jintae' )) | |
# Format Method | |
name = "jintae" | |
email = "jtjoo@me.com" | |
birthyear = 1983 | |
print( " {0}'s E-mail address is {1}, born in {2} ".format( name, email, birthyear ) ) | |
# Special Characters | |
print("jintae's \"Tutorial\"") | |
print("\\") | |
print("Hello\nworld") | |
print("Hello\tworld") | |
print("\a\b") | |
# Numbers and String | |
print(10+5) | |
print("10"+"5") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment