变量:存东西,为后面调用准备
定义: name = "zmg"
调用变量:print("My name is",name)
Python中路径名称是一类特殊情况需要特别说明。反斜线(\)是Python中保留的转义字符和行连接符,因此在定义路径变量时需要使用两个反斜线(\\)或者一个斜线(/)或是在使用单个反斜线时需增加字母 r 前缀。
Python中提供了很多字符串的操作方法。使用+运算符就可以将运算符两边的字符串合并为一个新的字符串变量:
mapSite = r"D:\map\jiangchuan" + ".shp" >>>>>>>>>
mapSite = r"D:\map\jiangchuan.shp"
print (
mapSite)
name = "zmg"
name2 = name
name = "km"
print(name,name2)
代码:
运行: km zmg
规则:只能是字母,数字,下划线的任意组合 第一个字符不能是数字
特殊字符,关键字不能声明为变量
驼峰式:zhouMingGang = "Life kunming"
用户交互:
username =input("name:")
password = int(input("word:"))
info ='''
---------info of name%------
name:%s
word:%d
'''%(username,username,password)
print(info)
代码:
注意:2.X raw_input 3.X input
在Python中同一变量可以动态赋值。Python解释器会动态地处理赋给变量的数据类型
强转整型int str字符串 打印类型print(type(password))
密文:
import getpass
_username="zmg"
_password="zmg12345"
username =input("name:")
password = input("word:") #密文:password = getpass.getpass("word:")
if _username == username and _password == password:
print("欢迎登录!")
else:
print("滚开!")
代码:
任性玩:
zhouMingGang = 28
count = 0
while count < 3:
guess = int(input("age:"))
if zhouMingGang == guess:
print("猜对了!")
break
elif guess > zhouMingGang:
print("往小的猜!")
else:
print("往大的猜!")
count+=1
if count == 3:
countime = input("你要继续玩?")
if countime != 'n':
count = 0
代码: