码迷,mamicode.com
首页 > 编程语言 > 详细

Python随笔(一)、python基础

时间:2017-11-18 23:45:16      阅读:443      评论:0      收藏:0      [点我收藏+]

标签:python

pycharm下设置自己的模板:

在File---settings---File and Code Templates---Python script 脚本里添加:

#!usr/bin/env python 

#-*- coding:utf-8 _*-  

""" 

@author:${USER} 

@file: ${NAME}.py 

@time: ${YEAR}/${MONTH}/${DAY} 

"""  

一、第一个python程序:

#!usr/bin/env python 
#-*- coding:utf-8 _*-  
"""
@author:Administrator
@file: HelloWorld.py
@time: 2017/11/{DAY}
"""
print("HelloWorld!!!")
print("你好,世界")

二、变量和赋值:
#!usr/bin/env python
#-*- coding:utf-8 _*-  
"""
@author:Administrator
@file: bianliang.py
@time: 2017/11/18
"""
#赋值
name = "chenjisong"
age = 30
print(name,age)

字符串类型的必须要加引号

a = 3
b = a
a = 5
print(a,b)

返回结果为(5,3)

解析:a = 3,内存地址指向3,b = a,则b = 3,此时a 和 b 都指向内存地址3,当 a = 5的时候,a 的内存地址指向了5,则a = 3 这个内存地址被回收了,但是b的内存地址未被回收,b仍然等于3,所以最后返回的结果是(5,3)

变量起名的原则:

      1、显示,通俗易懂

      2、驼峰写法(首字母大写)          例如:NumsOfJackGf

      3、下横线写法(不能为中横线)   例如:nums_of_jack_gf

      4、不能数字开头,但是可以在中间和结尾

      5、命名中不能有特殊字符

      6、变量的命名不能有空格

     7、关键字不能声明为变量

 内存地址的验证:

C:\Users\Administrator>python

Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)]

 on win32

Type "help", "copyright", "credits" or "license" for more information.

>>> import keyword

>>> a = 5

>>> b = a

>>> id(a),id(b)

(1363763552, 1363763552)

a 和 b的内存地址完全一样

>>> a = 10

>>> id(a),id(b)

>>> (1363763712, 1363763552)

当a的值改变之后,a的内存地址也发生了变化(是python中的内存地址,不是物理机器的内存地址)


三、用户交互


[root@python3 ~]# python
Python 3.6.3 (default, Nov 12 2017, 04:07:16)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> name = input("please input your name:")
please input your name:chenjisong

>>> print(name)

chenjisong

>>> a = 5
>>> eval(‘a‘)
5

四、条件判断与缩进

IF....ELSE和缩进


伪代码:

如果   你是富二代

          我们俩就拍拖

或者   你很努力上进

          我们可以接触试试

否则

          免谈


缩进要一致:
sex = input ("plsase input your gender:")
if sex == "gril":
   print("I would like to have a baby")
elif sex == "man":
   print("going to homesexual!")
else:
   print("Pervert!!!")

游戏:猜幸运数字:

#!usr/bin/env python 
#-*- coding:utf-8 _*-  
"""
@author:Administrator
@file: lucky_number.py
@time: 2017/11/18
"""
lucky_number = 18
guess_number = int(input("can you guess my lucky_number:"))
if guess_number > lucky_number:
   
print("guess_number is bigger then lucky_number")
elif guess_number < lucky_number:
   
print("guess_number is smaller then lucky_number:")
else:
   
print("congratulations,you guess it,but no prize")


五、循环控制:

break结束循环:(猜对即跳出循环,没猜对就一直猜)

while True:   
   
lucky_number = 18
   
guess_number = int(input("can you guess my lucky_number:"))
   if guess_number > lucky_number:
       
print("guess_number is bigger then lucky_number")
   elif guess_number < lucky_number:
       
print("guess_number is smaller then lucky_number:")
   else:
       
print("congratulations,you guess it,but no prize")
       break
while lucky_number != input_num:
   
input_num = int(input("input the guess num:"))
   if input_num > lucky_number:
       
print("the real number is smaller")
   elif input_num < lucky_number:
       
print("the real number is bigger")
   else:
print("bingo")


六、循环次数限制:

#!usr/bin/env python 
#-*- coding:utf-8 _*-  
"""
@author:Administrator
@file: lucky_number.py
@time: 2017/11/18
"""
lucky_number = 18
input_num=-1
guess_count = 0
#while lucky_number != input_num:
while guess_count < 3:
   input_num = int(input("input the guess num:"))
   print("guess count:",guess_count)
   if input_num > lucky_number:
       print("the real number is smaller")
   elif input_num < lucky_number:
       print("the real number is bigger")
   else:
       print("Bingo!")
       break
   guess_count += 1
else:
   print("try too many times")

两重判断:

第一重:三次猜不对直接退出(guess_count>3),打印“try too many times”

第二重:猜对了直接打印bingo,退出

for循环猜数字游戏:

#!usr/bin/env python 
#-*- coding:utf-8 _*-  
"""
@author:Administrator
@file: lucky_number.py
@time: 2017/11/18
"""
#while True:
lucky_number = 18
input_num=-1

for i in range(5):
   
input_num = int(input("input the guess num:"))
   if input_num > lucky_number:
       
print("the real number is smaller")
   elif input_num < lucky_number:
       
print("the real number is bigger")
   else:
       
print("Bingo!")
       break
else
:
   
print("try too many times")


七、常用数据类型

数据类型:

数字:

     int(整型)

     float(浮点型)

     long(长整型)

布尔:(True(1) 和  False(0))  真和假

字符串    str

列表        list

元祖       tuple

字典       dict

type可以查看数据类型:

>>> type(2**10)

<class ‘int‘>

>>> type(2.99)

<class ‘float‘>




本文出自 “linux” 博客,转载请与作者联系!

Python随笔(一)、python基础

标签:python

原文地址:http://chenjisong.blog.51cto.com/7858025/1983201

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!