最新の Python の紹介

8764 ワード

Python データ型
データ型は、Python でメモリ空間を割り当てるために使用される変数です. Pythonでは、変数にメモリ空間を手動で割り当てる必要は必ずしもありませんが、変数に値が割り当てられると、これは自動的に行われます.

文字列データ型



文字列は、一重引用符または二重引用符のいずれかの引用符で囲まれた一連の文字です.文字列変数を変更するたびに、新しい文字列オブジェクトが作成されます.


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’