현대 파이썬 소개

8741 단어
파이썬 데이터 유형
데이터 유형은 파이썬에서 메모리 공간을 할당하는 데 사용되는 변수입니다. 파이썬에서 변수에 대한 메모리 공간을 수동으로 할당할 필요는 없지만 값이 변수에 할당될 때 자동으로 발생합니다.

문자열 데이터 유형



문자열은 작은따옴표 또는 큰따옴표로 묶인 따옴표로 묶인 문자 시퀀스입니다. 문자열 변수를 변경할 때마다 새 문자열 개체가 생성됩니다.


type(name) #outputs ‘str’

##Set datatype##
Sets are unordered list of objects.
They are of two types:
-Sets
-Frozen sets
##Sets## 
Are immutable meaning new elements can be added or removed.


basket={“orange”,”banana”,”mango”}


print(basket) #outputs the element in basket variable

##Frozen Sets##
They are immutable meaning no modification can be done to it.


cities=frozenset({“Nairobi”,”Nakuru”,”Kisumu”,”Mombasa”})


print(cities) #it outputs the elements in the cities variable

##Number datatype##
They are four types in python: int, long, float, complex.
-```

marks=10 #integer

-```

longNumber=1234L #long

-```

floatNumber=1.236 #float

-```

ComplexNumber=12j #complex

##List Datatype##
List datatype is similar to array whereby they elements are enclosed in square brackets []; list can store elements of different types unlike array which stores element of the same type.


list1=[1,2,”abn”,1.3,”asdf”]”

##Tuple Datatype##
Tuples are similar to lists the difference is tuples cannot be changed and elements are enclosed in circular bracket ()


tuple1=(“hello”)


tuple2=(1,2,3, “Word”)

##Dictionary datatype##
Dictionary consists of key-value pairs. It is enclosed by curly braces {} and values can be assigned and accessed using square brackets[].


dic={'name':'red','age':10}


print(dic) #will output all the key-value pairs. {'name':'red','age':10}


print(dic['name']) #will output only value with 'name' key. 'red'

##Indentation##
Indentation is used in python instead of semi-colons. Indentation is  can be achieved by using tab key or four spaces.


class ExampleClass:

클래스에 속하는 모든 함수는 동일하게 들여쓰기되어야 합니다.


def init(self):


name = "example"


def someFunction(self, a):


#Notice everything belonging to a function must be indented


if a > 5:


return True


else:


return False

함수가 동일한 수준으로 들여쓰기되지 않으면 상위 클래스의 일부로 간주되지 않습니다.


def separateFunction(b):


for i in b:

루프도 들여쓰기되고 중첩 조건이 새 들여쓰기를 시작합니다.


if i == 1:


return True


else:


return False


separateFunction([2,3,5,6,1])

Functions
Function is a module consisting of definition and statement
To declare a function we use ```

def

``` keyword


def say_hello():


print("Hello!")

##Literals ##
A literal is a succinct and easily visible way to write a value. Literals represent the possible choices in primitive types for that language. Some of the choices of types of literals are often integers, floating point, Booleans and character strings. Python support the following literals:
-   String literals   ::   "halo" , '12345'
-   Int literals   ::   0,1,2,-1,-2
-   Long literals   ::   89675L
-   Float literals   ::   3.14
-   Complex literals   ::   12j
-   Boolean literals   ::   True or False
-   Special literals   ::   None
-   Unicode literals   ::   u"hello"
-   List literals   ::   [], [5,6,7]
-   Tuple literals   ::   (), (9,),(8,9,0)
-   Dict literals   ::   {}, {'x':1}
-   Set literals   ::   {8,9,10}
##Type conversion##
Is the process of converting a value of a particular data type to another datatype.


marks= 100


print(type(marks)) #returns int


print(str(marks)) #returns ‘100’




좋은 웹페이지 즐겨찾기