آموزش زبان برنامه نویسی پایتون#جلسه سوم
بسم الله الرحمن الرحیم
جلسه دوم
انواع داده در پایتون (Python)
جلسه دوم
نکاتی درباره انواع داده در پایتون (Python)
>>> True + True
2
>>> True - False
1
>>> True * False
0
>>> True / False
Traceback (most recent call last):
File "kpyshell#38>", line 1, in
True / False
zeroDivisionError: integer division or modulo by zero
>>>
>>> a=2
>>> type (a)
>>> b = 5.O
>>> isinstance ( int)
Traceback (most recent call last):
File "", line 1, in
isinstance ( int)
TypeError: is instance expected 2 arguments, got 1
>>> help (isinstance)
Help on built-in function is instance in module __builtin__ :
isinstance (. . . )
isinstance (object, class-or-type-or-tuple) -> bool
Return whether an object is an instance of a class or of a subclass thereof. With a type as second argument, return whether that is the object's type. The form using a tuple, is instance (x, (A, B, . . . )), is a shortcut for is instance (X, A) or is instance (x, B) or . . . (etc.).
>>> is instance (b, float)
True
>>>
>>> int (ー2・5)
-2
>>> float(3)
3.0
>>> type (1 + 5.0)
<type 'float’>
>>>داده های عددی در پایتون (Python)
>>> 11 / 2
5.5
>>> 11 // 2
5
>>> -11 // 2
-6
>>> 11.0 // 2
5.0
>>> 11 ** 2
121
>>>11% 2
1>>> a = -4
>>> +a
-4
>>> -a
4
>>>
ماژول fractions
>>> import fractions
>>> a = fractions. Fraction (1,3)
>>> a
Fraction (1, 3)
>>> a - 3
Fraction (-8, 3)
>>> fractions. Fraction (0,0)
Traceback (most recent call last):
File "<pyshell#103>", line 1, in <module>
fractions. Fraction (0,0)
File "C:\Python 27\lib\fractions.py", line 162, in__new__
raise ZeroDivisionError('Fraction(%s, O)' % numerator)
Zero Division Error: Fraction(0, 0)
>>> fractions.Fraction(0,5)
Fraction(0, 1)
>>>
>>> fractions. Fraction(10,5)
Fraction(2, 1)
ماژول math
>>> import math
>>> math.pi
3.141592653589793
>>> math.Sin(math.pi / 2)
1.O
>>> math.tan(math.pi / 4)
O.9999999999999999
رشته ها در پایتون (Strings)
توابع و متد های پایتون (Python)
>>> s = “python”
>>> s
‘python’
>>> a = 'learning'
>>> a
'learning'
>>> len(s)
6
>>> s.upper()
'PYTHON'
>>> a + s
'learningpython'
>>> a + ' ' + s
'learning python'
>>> session_no = 'first'
>>> course = 'python'
>>> "this is the {O} course of {1}". forma(session_no, course)
'this is the first course of python'
>>> "this is the {} course of {}".format(session_no, course)
"this is the first course of python"
>>>
>>> '{0:.if} {1}'. format(698.24, 'GB')
'698.2 GB'
>>> '{0:d} days ago was {1:s}'. format (2, 'Sunday')
'2 days ago was Sunday'
>>> query = 'user=Tom&database=master&password=1234'
>>> l= query.split ('&')
>>> l
['user=Tom', 'database=master', 'password=1234']
Slicing در پایتون
>>> a_str = 'python is an easy language'
>>> a_str[:]
'python is an easy language'
>>> a_str[0:6]
'python'
>>> a_str[:6]
'python'
>>> a_str[13:]
'easy language'
>>> a_str[-8: -1]
'languag'
>>> a_str.replace(a_str[0:6], 'c#')
'c# is an easy language'