Python入门简单操作

# -*- coding: utf-8 -*-
"""
Created on Fri Oct 12 15:40:37 2018

@author: Administrator
"""

#%%
import tensorflow as tf
import numpy as np
a = np.array([1,2,3,4,5,6,7,8,9],dtype='float32');
a = a.reshape(3,3);
c = a + [22,33,44];#c.dtype='float64'
c = c.astype(np.float32)
#c.dtype = 'float32'
print('c=',c);
condition = 1
while (condition < 10):
    print(condition)
    condition = condition + 1
example_list = [1,2,3,4,5,6,7,88,99]
for i in example_list:
    print(i)
for i in range(len(example_list)):
    print(i)
    print(example_list[i])
for i in range(2,10):
    print(i)
x = 1
y = 0
z = 3
if(x>y):
    print('x>y')
elif(x==y):
    print('x=y')
else:
    print('x<y')
    
def func(x,y=3):
    a = x;
    b = y;
    c = a + b;
    print(c)
    return a,b,c
d1,d2,d3=func(3,4)
#%%Variable
APPLE = 100
def func(x,y=3):
    a = x;
    b = y;
    
    print(a)
    print(APPLE)
func(3)
APPLE = APPLE + 100
print(APPLE)
func(3)
#%%文件写
text = 'this is my first test.\nthis is my second test.\nthis is my end test.\n'


filename = r'C:\Users\Administrator\Desktop\python\my file.txt'
my_file = open(filename,'w')
my_file.write(text)
my_file.close()
#%%文件append
text = 'this is my first test.\nthis is my second test.\nthis is my end test.\n'
append_text = 'scczyy520'

filename = r'C:\Users\Administrator\Desktop\python\my file.txt'
my_file = open(filename,'a')
my_file.write(append_text)
my_file.close()
#%%文件读
filename = r'C:\Users\Administrator\Desktop\python\my file.txt'
my_file = open(filename,'r')
read_text = my_file.read()
#read_text1 = my_file.readlines()
my_file.close()
print(read_text)
#print(read_text1)
#%%class 类 和 __init__ 初始化功能
class Calculator:
    name = 'Good calculator'
    price = 18
    def __init__ (self,name,price,height,width,weight):
        self.name = name
        self.price = price
        self.h = height
        self.wi = width
        self.we = weight
    def add(self,x,y):
        result = x + y
        print(result)
    def minus(self,x,y):
        result = x - y
        print(result)
    def times(self,x,y):
        print(x*y)
    def divide(self,x,y):
        print(x/y)
        
calcu = Calculator('Bad calculator',12,30,15,19)

#%% 元组和列表
#tuple list
a_tuple = (12,3,4,5)
another_tuple = 1,2,45
#list
a_list = [12,35,66]
for i in range(len(a_tuple)):
    print(a_tuple[i])
    
for i in range(len(a_list)):
    print(a_list[i])
#%%列表
a = [1,2,3,4,5]
a.append(0)
a.insert(0,0)
a.remove(2)
print(a.index(0))
#%%多维列表
a = [[1,2,3],[4,5,6],]
print(a[0][1])#2
#%%字典 dictionary
d = {'apple':1,
     'pear':2,
     'orange':3}
print(d['apple'])
del d['pear']
print(d)
d['b'] = 20
d['c'] = 20
print(d)
#%%载入自己的模块 import
#方法一
import time
print(time.localtime())
#方法二
import time as t
print(t.localtime())
#方法三
from time import time,localtime
print(time())
#方法四
from time import*
#%%载入自己的模块
import m1
print(10)

m1.aa()



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值