Skip to content

dannycju/basic_python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 

Repository files navigation

basic_python

First

For Python 2

print 'hello'

For Python 3

print('hello')

Comment

# print 'hello'

Data type

Python is dynamic typed.

x = 54
y = 5.4
s = 'hello'
b = True
print 'x: {}'.format(x)
print '{s} {x}'.format(s=s, x=x)
print '{sakura} {xmen}'.format(sakura=s, xmen=x)

List

a = [1, 234, 45, 45]
b = ['hello', 'python']
a[-1]
a[0:2]
s[0:4]
a[1] = a[1] - 1
b.append('world')
len(a)
a.count(456)
234 in a

Dictionary

d = {'key': 'Value', 'last': 'Suzuki'}
d['last']
d['first'] = 'Alto'

Tuple

t = (100, 200)

Control

if x == 54:
    print 'yes'
elif x == 5.4:
    print 'no'
else:
    print 'NO'
while x == 54:
    print 'yes'
for item in a:
    print item

Exception

z += 1
try:
    z += 1
except:
    z = 0
    z += 1

Exercises

# Replace space in string with underscore
s = 'sublime text'
w = s.split()
s = '_'.join(w)
print s
# Split string by symbol
s = '   this ;is;  so;  fun  '
w = s.split(';')
# ... and also cleanup the whitespaces
w = []
for r in s.split(';'):
    w.append(r.strip())
w = [r.strip() for r in s.split(';')]
# Make a copy of the list without duplicated numbers
a = [1, 2, 3, 3, 5, 5, 6]
b = []
...
# Count number of duplicate of each unique number in the list
a = [10, 10, 20, 20, 10, 20, 10, 30, 30, 40]

# Expected result: count = {10: 4, 20: 3, 30: 2, 40: 1}

Read and write to file

s = 'label_a,label_b,label_c'

with open('test.txt', 'w') as f:
    f.write(s + '\n')
with open('text.txt', 'r') as f:
    for line in f:
        print line

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published