An object-oriented language that is simple and easy to learn. It is adopted by Google App Engine, and there is a framework called Django.
$python
Hit to enter interactive mode.
python
>>>
If you write it here, it will be executed immediately. It looks like the following.
python
>>>print "hello world"
exit()
You can get out with.
hello.py
print "hello world"
Create and execute a file like
$python hello.py
msg = "hello world"
print msg
The output of hello world. Uppercase and lowercase letters are treated as different things.
Integer, decimal, complex Operator +-* / //% ** etc.
print 10 * 5 #50
print 10 // 3 #3
print 10% 3 #1
print 2**3 #8
“hello”
‘hello'
If you do like this, it will be a character string.
However, in the case of the Japanese, `u" Hello "`
put u like.
Line breaks are reflected when enclosed in "" "
print """<html lnag="ja">
<body>
</body>
</html>"""
Escapes like \ t \ n are available.
s = "abcdefgh"
#String length
print len(s)
#Character position
print s.find("c") #2 Counting from 0
#Cut out characters[Th:word count]
print s[2] #c
print s[2:5] #cde
print s[:5] #abcde
print s[2:-1] #cdefgh
Number <> string -Convert from string to number int float
print 5 + int("5") %10
print 5 + float("5") %10.0
-Convert from numerical value to character string str
age=20
print "i am" + str(age) + "years old!"
sales = [255, 100, 353, 400]
print len(sales) #4 Extract the length of the array
print sales[1] #Acquisition of 100 elements
print sales + sales #Joining arrays[255, 100, 353, 400, 255, 100, 353, 400]
print sales *3 #Array iteration[255, 100, 353, 400, 255, 100, 353, 400, 255, 100, 353, 400]
print 100 in sales #true in element existence judgment
range
Array creation
range(10) #[1,2,3,4,5,…8,9]Up to 9
range(3, 10) #[3,4,5,…8,9]From 3 to 9
range(3, 10, 2) #[3,5,7,9]Skip 2 from 3 to 9
#Sort in ascending order
sales.sort()
#Reverse the list
sales.reverse()
#String → list split
d = "2013/12/15"
print d.split("/") #['2013', '12', '15']
#List → string join
a = ['a', 'b', 'c']
print '-'.join(a) #a-b-c
It feels like a = (2, 5, 8). You can connect and repeat just by not being able to change the contents of the elements. → Prevent strange mistakes and increase the calculation speed.
#Tuple → list
b = list(a)
#List → Tuple
c = tuple(b)
a = set([1, 2, 3, 4, 3, 2])
b = set([3, 4, 5])
Do not allow duplicate elements
print a
Is 3,2 is duplicated and ignored.
#Difference set
print a - b
#Union
print a | b
#Only common terms
print a & b
#Items on only one side
print a ^ b
[2505, 523, 500]
{"apple":200, "orange":300, "banana":500}
print sales; #The order may change
print sales["apple"] #200
sales["orange"] = 800 #Change value
print sales #{'orange': 800, 'apple': 200, 'banana': 500}
print "orange" in sales #true Existence judgment
print sales.keys() #key list
print sales.values() #list of values
print sales.items() #key-list of values
a = 10
b = 1.232323
c = "apple"
d = {"orange":200, "banana":500}
print "age: %d" % a
print "age: %10d" % a #10 digits
print "age: %010d" % a #Fill with 10 digits 0
print "price: %f" % b
print "price: %.2f" % b
print "name: %s" % c
print "sales: %(orange)d" %d
print "%d and %f" % (a, b)
scoe = 70
if score > 60:
print "ok!"
print "OK!"
#Comparison operator> < >= <= == !=
#Logical operator and or not
if score > 60 and score < 80:
print "ok!"
print "OK!"
if 60 < score <80
print "ok!"
print "OK!"
score = 70
if score > 60:
print "ok!"
elif score > 40:
print "soso…"
else:
print "ng!"
print "OK!" if score > 60 else "NG!"
sales = [13, 43, 4233, 2343]
for sale in sales:
sum += sale
else:
print sum
#* You can write a process to be executed only once when the for statement is exited with the else of the for statement.
for i in range(10)
if i == 3:
continue #Skip the loop
if i == 3:
break #End the loop
print i
#From 0 to 9.
for i in range(1, 101)
#processing
#i loops from 1 to 100
users = {"apple":200, "orange":300, "banana":500}
for key, value in users.iteritems()
print "key: %s value: %d" % (key, value)
for key, value in users.iterkeys()
print key
for value in users.itervalues()
print value
n = 0
while n < 10
print n
n = n + 1
n += 1
else:
print "end"
#continue, break,else is the same as the for statement.
#If you use break, the else clause will not be executed either.
def hello():
print "hello"
hello()
#argument
def hello(name, num = 3):
print "hello %s" %name * num
hello(name = "tom", num =2) #If you give it a name, you can change the order of the arguments.
hello("steve")
def hello(name, num = 3):
return "hello %s" %name * num
s = hello("steve")
print s
name = "apple"
def hello():
name = "orange"
print name
#pass
def hello2():
pass
#If you create a function called hello2 for the time being and write pass when the contents are empty, it's okay.
#In other languages,{}You can indicate the end of the function so that it can be displayed, but in python you can not indicate the end, so write pass instead.
#map-Execute a function for each element of the list
def double(x):
return x*x
print map(double, [2, 5, 8])
#lamda anonymous function-used when you want to use the function directly when using map
print map(lambda x:x*x, [2, 5, 8])
Object (a collection of variables and functions) Class: Object blueprint Instance: A materialized class
class User(object):
def __init__(self, name):
self.name = name
def greet(self):
print "my name is %s" % self.name
bob = User("Bob")
tom = User("Tom")
print bob.name
bob.greet()
tom.greet()
class SuperUser (User):
def shout(self):
print :"%s is SuPER!" % self.name
tom = SuperUser("Tom")
tom.shout()
import math, random
from datetime import date
print math.ceil(5.2) #6.0
for i in range(5)
print random.random()
print date.today()
Introduction to Dot Install Python
Recommended Posts