Exemplo n.º 1
0
help(a.split)
a.split(',')
print(a)

# 调用模块中的变量和函数
# 同及目录下调用模块需要将该文件夹设置为Source Root,并且在python console中添加该Source Root.
# 方法一
import some_module

result = some_module.f(5)
pi = some_module.PI

# 方法二
from some_module import f, g, PI

result1 = g(5, PI) + f(2)

# 方法三  别名的使用
import some_module as sm
from some_module import PI as pi, g as gf

r1 = sm.f(5)
r2 = gf(6, pi)

# 用is关键字判断两个引用是否指向同一个对象
a = [1, 2, 3]
b = a
c = list(a)  # list函数始终会创建新列表,即c是新生成的列表
a is b  # 返回True
a is not c  # 返回True
a == c  # 返回True
Exemplo n.º 2
0
		return True
	except TypeError:
		return False
isiterable('a string')
isiterable('[1, 2, 3]')
isiterable(5)

if not isinstance(x, list) and isiterable(x):
	x = list(x)

import some_module #some_module.py
result = some_module.f(5)
pi = some_module.PI 

from some_module import f,g,PI
result = g(5, PI)

import some_module as sm
from some_module import PI as pi, g as gf
r1 = sm.f(pi)
r2 = gf(6, pi)

#Most of the binary math operations and comparisons are as you might expect:
a is b  #True if a and b reference the same Python object
a is not b #True if a and b reference different Python objects

#strings and tuples are immutable
#Scalar type in Python
str, None, bytes, float, bool, int
c = """
This is a longer string that
Exemplo n.º 3
0
    print(x) # now its a list!


## imports ---------------------------------------------------------
cd /Users/dnoriega/GitHub/pyfordata/appendix

# save a file `some_module.py` for import
import some_module
result = some_module.f(5)
pi = some_module.PI
# ??some_module.g # we can look at the functions
# ??some_module.f # etc.

# equivalently
from some_module import f, g, PI
result = g(5, PI)

# by using `as` keywords, we can give imports different values
import some_module as sm
from some_module import PI as pi, g as gf

r1 = sm.f(pi) # some_module as `sm`
r2 = gf(6, pi) # PI as `pi`, g as `gf`


## binary operators and comparisons --------------------------------

5 > 7 # False
5 < 7 # True

# to check references, use `is` and `is not`
Exemplo n.º 4
0
'''
@Author     : Md. Shamimul Islam
@Written    : 08/02/2019
@Description: python Import from module 
'''
import some_module

result = some_module.f(4)
pi = some_module.Pi

print(result)
print(pi)

#equivalently
from some_module import f, g, Pi
result01 = g(result, pi)
print(result01)

#equivalently
import some_module as sm
from some_module import Pi as p, g as gf
result02 = sm.f(9)
result03 = sm.gf(2, p)

print(result02)
print(result03)
Exemplo n.º 5
0
print(isiterable(
    [1, 2, 3]))  #isiterable 함수에 배열 [1,2,3] 대입 => 배열이므로  iterable 함 따라서 true 반환
print(isiterable(
    5))  #isiterable 함수에 변수 5 대입 => 변수 하나 이므로, not iterable 함 따라서 false 반환

# B03.The Basics. Imports
import some_module  #some_module.py를 import

result = some_module.f(5)  #result에 some_module에 선언 되어있는 함수 f에 5를 대입
print(result)  #result값 출력
pi = some_module.PI  #pi에 some_module에 선언 되어있는 변수 PI를 대입
print(pi)  #pi값 출력

from some_module import f, g, PI  #some module 모듈로 부터 f,g, PI를 불러옴
result = g(5, PI)  #result에 함수 g에 5와 PI를 대입한 결과 값을 대입
print(result)  #result 값 출력

import some_module as sm  #module some_module을 sm이라는 이름으로 선언
from some_module import PI as pi, g as gf  #some_module의 변수 PI를 pi로, 함수 g를 gf로 선언

r1 = sm.f(pi)  #바뀐 이름 sm을 가진 모듈 sm의 함수 f에 pi 값 대입
print(r1)  #r1 출력
r2 = gf(6, pi)  #함수 gf에 매개변수로 6과 pi 대입
print(r2)  #r2 출력

# B04.The Basics. Binary operators and comparisons

print(5 - 7)  #5-7 출력
print(12 + 21.5)  #12+21.5 출력
print(5 <= 2)  #5가 2보다 작거나 같은지  bool로 출력
results = []
for line in file_handle:
    # keep the empty lines for now
    # if len(line) == 0:
    #   continue
    results.append(line.replace('foo', 'bar'))


# #### Function and object method calls

# In[ ]:


result = f(x, y, z)
g()


# In[ ]:


obj.some_method(x, y, z)


# In[ ]:


result = f(a, b, c, d=5, e='foo')


# #### Variables and pass-by-reference