python_one-day

python入门_(1)


作者:_晓冬

归档:学习笔记

2017/9/9

目  录

第1章 练习... 1

1.1 格式化输出... 1

1.2 流程控制if..else. 1

1.3 流程控制while. 2

第2章 作业题... 3

2.1 简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释型... 3

2.2 执行
Python 脚本的两种方式是什么... 3

2.3 Pyhton 单行注释和多行注释分别用什么?. 3

2.4 布尔值分别有什么?. 4

2.5 声明变量注意事项有那些?. 4

2.6 如何查看变量在内存中的地址?. 4

2.7 代码... 4

2.8 写出一下代码... 4

2.8.1 a. 使用while循环实现输出2-3+4-5+6...+100 的和... 4

2.8.2 b. 使用 while 循环实现输出 1,2,3,4,5, 7,8,9, 11,12 使用
while 循环实现输出 1-100 内的所有奇数... 5

2.8.3 e. 使用 while 循环实现输出 1-100 内的所有偶数... 5

2.9 现有如下两个变量,请简述
n1 和 n2 是什么关系?. 6

2.10 编写登陆接口... 6

第1章 练习

1.1 格式化输出


课上练习

name=input (‘your name: ‘)
age=input (‘your age: ‘)
print (‘my name is %s,my age is %s‘ %(name,age))

l  课后练习

l  Name=input(‘Name :‘)
Age=input(‘Age  :‘)
Sex=input(‘Sex  :‘)
Job=input(‘Job  :‘)
print (‘Name : %s ‘ ‘Age : %s ‘ ‘Sex : %s ‘ ‘Job : %s ‘ %(Name ,Age ,Sex ,Job) )

Name :wxd

Age  :23

Sex  :man

Job  :it

Name : wxd Age : 23 Sex : man Job : it

1.2 流程控制if..else

l  课上练习

age_of_girl=18
height=171
weight=99
is_pretty=True
if age_of_girl >= 18 and age_of_girl < 22 and height > 170 and weight < 100 and is_pretty == True:
    print(表白....‘)
else :
    print(阿姨好)

l  课下练习

today=input(‘>>: ‘)
if today == ‘Monday‘ :
    print (‘work on‘)
elif today == ‘Wednesday‘ :
    print(‘work on‘)
elif today==‘Thursday‘ :
    print(‘work on‘)
elif today ==‘Friday‘ :
    print(‘work on‘)
elif today==‘saturday‘ :
    print(‘go away‘)
elif today==(‘sunday‘) :
    print(‘go happy‘)

else:
     print (‘‘‘you mast print one day :
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday
    Sunday
    ‘‘‘)

1.3 流程控制while

l  输入用户和密码正确后操作退出

name=‘wxd‘
password=‘123‘

while True:
    input_name=input(‘user: ‘)
    input_pwd=input(‘key: ‘)
    if input_name == name and input_pwd == password:
        while Trwxdue:
            cmd=input(‘>>: ‘)
            if not cmd:continue
            if cmd == ‘quit‘ :
                break
                print(‘run <%s>‘ %cmd)
    else:
        print(‘usr and key is wrong‘)
        continue
    break

第2章 作业题

2.1 简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释型

l  编译型语言

在程序执行之前,有一个单独的编译过程,将程序翻译成机器语言,以后执行这个程序的时候,就不用再进行翻译了。

l  解释型语言

是在运行的时候将程序翻译成机器语言,所以运行速度相对于编译型语言要慢。

l  常见类型

C/C++ 等都是编译型语言,而Java,C#等都是解释型语言。

l  常用语言解释

虽然Java程序在运行之前也有一个编译过程,但是并不是将程序编译成机器语言,而是将它编译成字节码(可以理解为一个中间语言)。在运行的时候,由JVM将字节码再翻译成机器语言。

注:脚本语言一般都有相应的脚本引擎来解释执行。 他们一般需要解释器才能运行。

JAVASCRIPT,ASP,PHP,PERL,Nuva都是脚本语言。C/C++编译、链接后,可形成独立执行的exe文件。

脚本语言是一种解释性的语言,例如vbscript,javascript,installshield script,ActionScript等等,它不象c\c++等可以编译成二进制代码,以可执行文件的形式存在.

脚本语言不需要编译,可以直接用,由解释器来负责解释。

脚本语言一般都是以文本形式存在,类似于一种命令.

l  举例子说明:

如果你建立了一个程序,叫aaa.exe,可以打开.aa为扩展名的文件.

你为.aa文件的编写指定了一套规则(语法),当别人编写了.aa文件后,你的程序用这种规则来理解编写人的意图,并作出回应.那么,这一套规则就是脚本语言

2.2 执行 Python 脚本的两种方式是什么

l  利用Python执行脚本路径方式

l  放到命令行中解释器解释

2.3 Pyhton 单行注释和多行注释分别用什么?

l  #井号单行注释

l  """ /‘‘‘  单引号或双引号 多行注释

2.4 布尔值分别有什么?

True  false

2.5 声明变量注意事项有那些?

l  变量只能是字母、下划线  数字的组合

l  不能以数字开头

l  不能使用系统命令作为变量名

2.6 如何查看变量在内存中的地址?

id(vale)

2.7 代码

实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败!

实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次

实现用户输入用户名和密码,当用户名为 seven 或 alex 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次

cou=0

while cou < 3:

user=input(‘user: ‘)

passwd=input(‘passwd: ‘)

if (user==‘seven‘ or user==‘alex‘) and passwd==‘123‘:

print(‘登录成功‘)

break

else:

print(‘登录失败‘)

cou+=1

2.8 写出一下代码

2.8.1 a. 使用while循环实现输出2-3+4-5+6...+100 的和

aaa = ‘‘

# 定义计算结果

bbb = 0

for i in range(1,100):

aaa += str(i)

if i % 2 == 0:

aaa += ‘+‘

bbb -= i

else:

aaa += ‘-‘

bbb += i

print(‘字符串输出: \r\n %s \r\n计算结果: \r\n %s‘ % (aaa.rstrip(‘-‘), bbb))

2.8.2 b. 使用 while 循环实现输出 1,2,3,4,5, 7,8,9, 11,12 使用 while 循环实现输出 1-100 内的所有奇数

count=1

while count <=12:

if count == 6 or   count == 10:

count+=1

continue

print(count)

count+=1

count=1

while count <= 100:

if count %2 != 0:

print(count)

count+=1

2.8.3 e. 使用 while 循环实现输出 1-100 内的所有偶数

count=1

while count <= 100:

if count %2 != 0:

count+=1

continue

print(count)

count+=1

count=1

while count <= 100:

if count %2 == 0:

print(count)

count+=1

2.9 现有如下两个变量,请简述 n1 和 n2 是什么关系?

n1 = 123456

n2 = n1

解释:n1的值在内存存在,n2的值是指向n1的值,如下图

2.10 编写登陆接口

基础需求:

l  让用户输入用户名密码

l  认证成功后显示欢迎信息

l  输入三次

count=0

while count <3:

username=input(‘username: ‘)

password=input(‘password: ‘)

if username==‘xiechao‘ and password == ‘123‘:

print(‘登录成功‘)

break

else:

print(‘用户名或密码错误‘)

count+=1

时间: 2024-08-02 06:18:52

python_one-day的相关文章

python_one

1.使用方法title()来将姓名设置成合适的格式. full_name.title() 2.删除空白:删除开头和末尾的空白.rstrip() a= '    python   ' a = a.rstrip() echo a 3.python3中把需要打印的内容放到括号中,python3中的print是一个函数,因此括号必不可少. 4.列表: append()动态的增加列表中的元素,增加到末尾之后 motorcycles = [] motorcycles.append('home') inser

SQLAlchemy 操作数据库

首先安装 SQLAlchemy sudo pip3.4 install PyMySQL sudo pip3.4 install sqlalchemy 代码: 1 #!/usr/bin/env python 2 # encoding: utf-8 3 """ 4 @author: 侠之大者kamil 5 @file: Sqlalchemy_test1.py 6 @time: 16/4/11 下午10:41 7 """ 8 #from sqlalch