首页 > 编程语言 >python 入门

python 入门

时间:2023-05-22 19:04:38浏览次数:47  
标签:入门 intb python else str time print total


python 入门

#!/usr/bin/python
#coding=utf-8
if True:
    print "True"
else:
    print "False"
    print "hello world"

#total = item_one + item_two + item_three
days = ['Monday', 'Tuesday', 'wednesday']
print days
print 'hwllo world'

#if expression:
#    print "1"
#elif expression1:
#    print "2"
#else expression3:
#    print "3"

counter = 100
miles = 100.0
name = "jeffasd"
print counter
print miles;
print name
a = b = c = 2
a, b, c = 1, 2, "jeffasd"

var1 = 1;
del var1;

str = "hello world"


print str;
print str[0]
print str[2:4]
print str[2:]
print str * 2
print str + "hwllow"


#Python列表
list = ['runoob', 789, 2.23, 'josh', 30.2]
tinylist = [232, 'john']
print list
print list[0]
print list[1:3]
print list[2:]
print tinylist *2
print list + tinylist

#Python元组
#元组用"()"标识。内部元素用逗号隔开。但是元组不能二次赋值,相当于只读列表。
tuple = ('runoob', 'sfdsf', 89, 23.3)
tupleTuple = (1232, 'josh')
print tuple;
print tuple *2
print tuple + tupleTuple
#元组不允许赋值
#tuple[2] = 23

#python字典
dict = {}
dict['one'] = "THis is one"
dict[2] = "This is two"
tinyDict = {'name':"value", "key":"value2"}
print dict
print dict['one']
print dict[2]
print tinyDict
print tinyDict.keys()
print tinyDict.values()

inta = 1
intb = 2
if (inta == intb):
    print "相等"
if inta != intb :
    print "不相等"

if inta and intb :
    print "and"
if inta or intb :
    print "or"

if not inta and intb :
    print "not and"

if not False :
    print "not and -"

intArray = [1, 2, 3, 4, 5]
if intb in intArray :
    print "在数组内"
if inta is intb :
    print "相等"
if inta is not intb :
    print "不相等"

flag = False
name = 'luren'
if name == "python" :
    flag = True
    print "welcome"
else:
    print name

count = 0
while count < 9 :
    count += 1
    print count

for letter in 'python' :
    print "当前字母是:", letter

fruits = ['banana', 'apple', 'mango']
for index in range(len(fruits)) :
    print '当前水果:', fruits[index]

#循环使用 else 语句
#在 python 中,for … else 表示这样的意思,for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的)的情况下执行,while … else 也是一样。
for num in range(10,20):  # 迭代 10 到 20 之间的数字
    for i in range(2,num): # 根据因子迭代
        if num%i == 0:      # 确定第一个因子
            j=num/i          # 计算第二个因子
            print '%d 等于 %d * %d' % (num,i,j)
            break            # 跳出当前循环
    else:                  # 循环的 else 部分
        print num, '是一个质数'

#判断素数比较牛逼的算法
i = 2
while(i < 100):
    j = 2
    while(j <= (i/j)):
        if not(i%j): break
        j = j + 1
    if (j > i/j) : print i, " 是素数"
    i = i + 1

print "Good bye!"

#Python pass是空语句,是为了保持程序结构的完整性。
#pass 不做任何事情,一般用做占位语句。
#Python 语言 pass 语句语法格式如下:
for letter in 'python':
    if letter == 'b':
        pass
        print '这是pass块'
    print '当前字母:',letter

import time
ticks = time.time()
print ticks

localTime = time.localtime(time.time())
print localTime

localtime = time.asctime(time.localtime(time.time()))
print localtime

def printime(str):
    print str
    return

printime("hwllo");

#不定长参数
def printInfo(arg1, *vartuple):
    print arg1
    for var in vartuple:
        print var
    return

printInfo(10)
printInfo(60, 10, 40)

#匿名函数
sum = lambda arg1, agr2: arg1 + agr2
print sum(10, 20)
print sum(20, 30.4)

#return 语句return语句[表达式]退出函数,选择性地向调用方返回一个表达式。不带参数值的return语句返回None。
def sum(agr1, agr2):
    total = agr1 + agr2
    print total
    return total
total = sum(3, 5)
print total

#全局变量和局部变量
total = 0; # 这是一个全局变量
# 可写函数说明
def sum( arg1, arg2 ):
    #返回2个参数的和."
    total = arg1 + arg2; # total在这里是局部变量.
    print "函数内是局部变量 : ", total
    return total;

#调用sum函数
sum( 10, 20 );
print "函数外是全局变量 : ", total

import math
content = dir(math)
print content




标签:入门,intb,python,else,str,time,print,total
From: https://blog.51cto.com/u_16124099/6326578

相关文章

  • 利用Python爆破数据库备份文件
    某次测试过程中,发现PHP备份功能代码如下://根据时间生成备份文件名$file_name='D'.date('Ymd').'T'.date('His');$sql_file_name=$file_name.'.sql';那么形成的文件名格式如:D20180118T101433.sql,理论上是可以爆破的,于是写了一段Python脚本来尝试爆破。Py......
  • python - selenium + Edge
    1.安装相关库和下载相关文件pip3installseleniumpip3installmsedge-selenium-tools在https://developer.microsoft.com/zh-cn/microsoft-edge/tools/webdriver/下载msedgedriver.exe,可在edge帮助查看当前edge的版本号,下载对应版本即可2.代码fromseleniumimportwe......
  • VUE入门
    什么是Vue?Vue(发音为/vjuː/,类似 view)是一款用于构建用户界面的JavaScript框架。它基于标准HTML、CSS和JavaScript构建,并提供了一套声明式的、组件化的编程模型,帮助你高效地开发用户界面。无论是简单还是复杂的界面,Vue都可以胜任。vue的两大核心声明式渲染:Vue......
  • python 安装pip
    目录python安装pip下载get-pip.py脚本运行get-pip.py脚本pip使用python安装pip在安装Python库时,常用的工具是pip(Python包管理器)。如果你在使用Python时还没有安装pip,你可以按照以下步骤安装:下载get-pip.py脚本你可以从官方网站https://bootstrap.pypa.io/get-......
  • 矩阵入门
    矩阵向量与矩阵在线性代数中,向量分为列向量和行向量。向量也是特殊的矩阵,行向量可以看作是一个\(1\timesn\)的矩阵,例如下面这样:\[\begin{bmatrix}1&2&3&4&5\end{bmatrix}\]列向量可以看作是一个\(n\times1\)的矩阵,例如下面这样:\[\begin{bmatrix}1\\2\\3\\4\\5\e......
  • mkvirtualenv创建虚拟环境指定python版本号
    mkvirtualenv创建的虚拟环境,不指定具体的python版本时,默认使用的python版本是添加的环境变量中设置的pyhton版本。mkvirtualenv可以指定python版本,如:mkvirtualenv--python=python3.10venvname#venvname虚拟环境名称或者mkvirtualenv-ppython3.10venvname#venvna......
  • Python的基础语法(五)“数据内置方法之字典、元组、集合”
    字典的内置方法1、字典常用的定义方法:info={'name':'tony','age':18,'sex':'male'}info=dict(name='tony',age='18')2、字典的内置方法2.1、按照key取值,可存可取:dic={......
  • Python多进程运行——Multiprocessing基础教程2
    转载:Python多进程运行——Multiprocessing基础教程2-知乎(zhihu.com)1数据共享在多进程处理中,所有新创建的进程都会有这两个特点:独立运行,有自己的内存空间。我们来举个例子展示一下:importmultiprocessing#emptylistwithglobalscoperesult=[]defsquare_l......
  • 聊聊python的字符编码
    什么是字符编码?在计算机内部,所有的数据都是二进制形式存储的,无法直接存储我们人类的语言文字符号等,所以我们需要制定一种转换规则来明确计算机内部二进制与我们的数字符号文字之间的对应关系,这就出现了‘字符编码’。字符编码的发展史阶段一现代计算机起源于美国,所以......
  • 比较不同Python图形处理库或图像处理库的异同点
    python的图像处理库有很多种比如:pillow库、Numpy库、Scipy库、opencv库、pgmagic库等其中较常用的是NUmapy库、pillow库、openCV库,今天我们就这三种图像处理库来进行比较首先是numapy库;他是一个python库可以帮助我们处理所有类型的科学计算,他是在执行任何数据预处理或数据科......