Three .Python variables, constants, comments

 1. Run the python code.

Create a t1.py file contents in the disc d is:

print('hello world')

Open windows command line input cmd, written into the code determining the python d: t1.py 

You've run your first python program, namely: terminal ----> cmd -----> python file path. Enter get ~

2. interpreter.

Previous execute python d: When t1.py, explicitly pointed out t1.py script to be executed by the python interpreter.

If you want to execute a shell script similar to the same run python script, for example:  ./t1.py , then you need to specify the interpreter in the head hello.py file, as follows:

1
2
3
#!/usr/bin/env python
  
print  "hello,world"

As a result, execution: /t1.py can be.

ps: to be given t1.py execute permissions before execution, chmod 755 t1.py

3. comments.

When the line comments: # annotated content

Multi-line comments: '' 'is the annotation content' '' or '' 'is the annotation content "" "

4. Variable

What variables? Variable: the middle of running a temporary result of the presence of memory, so that subsequent code calls.

4.1, declare variables

lux = 'Lu I'

The code declares a variable called: lux, the variable name value: "Lu Xun himself"

The role of variables: the nickname, which refers to the generation of memory stored in an address in content

4.2, variables defined rules:

  • Variable names can be any combination of letters, numbers or underscore
  • The first character variable names can not be digital
  • The following keywords can not be declared variable name
    [ 'and', 'as' ,' assert ',' break ',' class', 'continue', 'def', 'del', 'elif', 'else', ' except ',' exec ',' finally ',' for ',' from ',' global ',' if ',' import ',' in ',' is', 'lambda', 'not', 'or' , 'pass', 'print' , 'raise', 'return', 'try', 'while', 'with', 'yield']
  • It may be defined to have variable descriptive.

4.3, the recommended way to define

Copy the code
# Hump body 

AgeOfOldboy = 56 is 

= NumberOfStudents 80 

# underlined 

age_of_oldboy = 56 is 

number_of_students = 80
Copy the code

Which do you think more clearly, which is the official recommendation, I'm sure you will first of two kinds, the first AgeOfOldboy first glance thought it was AngelaBaby

Assignment 4.4, variables

lux = 'Lu I', 
name = 'Taibaijinxing'

name1 = '太白金星'
name2 = name1
name3 = name2

4.5、定义变量不好的方式举例

  • 变量名为中文、拼音
  • 变量名过长
  • 变量名词不达意

5. 常量

常量即指不变的量,如pai 3.141592653..., 或在程序运行过程中不会改变的量

举例,假如老男孩老师的年龄会变,那这就是个变量,但在一些情况下,他的年龄不会变了,那就是常量。在Python中没有一个专门的语法代表常量,程序员约定俗成用变量名全部大写代表常量

AGE_OF_OLDBOY = 56

在c语言中有专门的常量定义语法,const int count = 60;一旦定义为常量,更改即会报错

6. 基础数据类型(初始)。

什么是数据类型?

  我们人类可以很容易的分清数字与字符的区别,但是计算机并不能呀,计算机虽然很强大,但从某种角度上看又很傻,除非你明确的告诉它,1是数字,“汉”是文字,否则它是分不清1和‘汉’的区别的,因此,在每个编程语言里都会有一个叫数据类型的东东,其实就是对常用的各种数据类型进行了明确的划分,你想让计算机进行数值运算,你就传数字给它,你想让他处理文字,就传字符串类型给他。Python中常用的数据类型有多种,今天我们暂只讲3种, 数字、字符串、布尔类型

6.1、整数类型(int)。

int(整型)

在32位机器上,整数的位数为32位,取值范围为-2**31~2**31-1,即-2147483648~2147483647

在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807

除了int之外, 其实还有float浮点型, 复数型,但今天先不讲啦

6.2、字符串类型(str)。

在Python中,加了引号的字符都被认为是字符串!

Copy the code
>>> name = "Alex Li" #双引号
>>> age = "22"       #只要加引号就是字符串
>>> age2 = 22          #int
>>> 
>>> msg = '''My name is taibai, I am 22 years old!'''  #我擦,3个引号也可以
>>> 
>>> hometown = 'ShanDong'   #单引号也可以
Copy the code

那单引号、双引号、多引号有什么区别呢? 让我大声告诉你,单双引号木有任何区别,只有下面这种情况 你需要考虑单双的配合

msg = "My name is Alex , I'm 22 years old!"

多引号什么作用呢?作用就是多行字符串必须用多引号

Copy the code
msg = '''
今天我想写首小诗,
歌颂我的同桌,
你看他那乌黑的短发,
好像一只炸毛鸡。
'''
print(msg)
Copy the code

字符串拼接

数字可以进行加减乘除等运算,字符串呢?让我大声告诉你,也能?what ?是的,但只能进行"相加"和"相乘"运算。

Copy the code
>>> name
'Alex Li'
>>> age
'22'
>>> 
>>> name + age  #相加其实就是简单拼接
'Alex Li22'
>>> 
>>> name * 10 #相乘其实就是复制自己多少次,再拼接在一起
'Alex LiAlex LiAlex LiAlex LiAlex LiAlex LiAlex LiAlex LiAlex LiAlex Li'
Copy the code

注意,字符串的拼接只能是双方都是字符串,不能跟数字或其它类型拼接

Copy the code
>>> type(name),type(age2)
(<type 'str'>, <type 'int'>)
>>> 
>>> name
'Alex Li'
>>> age2
22
>>> name + age2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects #错误提示数字 和 字符 不能拼接
Copy the code

6.3、布尔值(True,False)。

布尔类型很简单,就两个值 ,一个True(真),一个False(假), 主要用记逻辑判断

但其实你们并不明白对么? let me explain, 我现在有2个值 , a=3, b=5 , 我说a>b你说成立么? 我们当然知道不成立,但问题是计算机怎么去描述这成不成立呢?或者说a< b是成立,计算机怎么描述这是成立呢?

没错,答案就是,用布尔类型

Copy the code
>>> a=3
>>> b=5
>>> 
>>> a > b #不成立就是False,即假
False
>>> 
>>> a < b #成立就是True, 即真
True
Copy the code

 7. 程序交互

Copy the code
#!/usr/bin/env python
# -*- coding: utf-8 -*-
  
# 将用户输入的内容赋值给 name 变量
name = input("请输入用户名:")
  
# 打印输入的内容
print(name)
Copy the code

执行脚本就会发现,程序会等待你输入姓名后再往下继续走。

可以让用户输入多个信息,如下

Copy the code
#!/usr/bin/env python
# -*- coding: utf-8 -*-
name = input("What is your name?")
age = input("How old are you?")
hometown = input("Where is your hometown?")

print("Hello ",name , "your are ", age , "years old, you came from",hometown)
Copy the code

8. 流程控制之--if。

  假如把写程序比做走路,那我们到现在为止,一直走的都是直路,还没遇到过分叉口,想象现实中,你遇到了分叉口,然后你决定往哪拐必然是有所动机的。你要判断那条岔路是你真正要走的路,如果我们想让程序也能处理这样的判断怎么办? 很简单,只需要在程序里预设一些条件判断语句,满足哪个条件,就走哪条岔路。这个过程就叫流程控制。

if...else 语句

单分支

if 条件:
    满足条件后要执行的代码

双分支

Copy the code
"""
if 条件:
    满足条件执行代码
else:
    if条件不满足就走这段
"""
AgeOfOldboy = 48

if AgeOfOldboy > 50 :
    print("Too old, time to retire..")
else:
    print("还能折腾几年!")
Copy the code

缩进

这里必须要插入这个缩进的知识点

你会发现,上面的if代码里,每个条件的下一行都缩进了4个空格,这是为什么呢?这就是Python的一大特色,强制缩进,目的是为了让程序知道,每段代码依赖哪个条件,如果不通过缩进来区分,程序怎么会知道,当你的条件成立后,去执行哪些代码呢?

在其它的语言里,大多通过{}来确定代码块,比如C,C++,Java,Javascript都是这样,看一个JavaScript代码的例子

Copy the code
var age = 56
if ( age < 50){
  console.log("还能折腾")
    console.log('可以执行多行代码')
}else{
   console.log('太老了')
}
Copy the code

在有{}来区分代码块的情况下,缩进的作用就只剩下让代码变的整洁了。

Python是门超级简洁的语言,发明者定是觉得用{}太丑了,所以索性直接不用它,那怎么能区分代码块呢?答案就是强制缩进。

Python的缩进有以下几个原则:

  • 顶级代码必须顶行写,即如果一行代码本身不依赖于任何条件,那它必须不能进行任何缩进
  • 同一级别的代码,缩进必须一致
  • 官方建议缩进用4个空格,当然你也可以用2个,如果你想被人笑话的话。

多分支

回到流程控制上来,if...else ...可以有多个分支条件

Copy the code
if 条件:
    满足条件执行代码
elif 条件:
    上面的条件不满足就走这个
elif 条件:
    上面的条件不满足就走这个
elif 条件:
    上面的条件不满足就走这个    
else:
    上面所有的条件不满足就走这段
Copy the code

写个猜年龄的游戏吧

Copy the code
age_of_oldboy = 48

guess = int(input(">>:"))

if guess > age_of_oldboy :
    print("猜的太大了,往小里试试...")

elif guess < age_of_oldboy :
    print("猜的太小了,往大里试试...")

else:
    print("恭喜你,猜对了...")
Copy the code

上面的例子,根据你输入的值不同,会最多得到3种不同的结果

再来个匹配成绩的小程序吧,成绩有ABCDE5个等级,与分数的对应关系如下

A    90-100
B    80-89 C 60-79 D 40-59 E 0-39 

要求用户输入0-100的数字后,你能正确打印他的对应成绩

Copy the code
score = int (input ( "Enter the score:")) 

IF Score> 100: 
    Print ( "I rub, the highest score before ... 100") 
elif Score> = 90: 
    Print ( "A") 
elif Score> = 80 : 
    Print ( "B") 
elif Score> = 60: 
    Print ( "C") 
elif Score> = 40: 
    Print ( "D") 
the else: 
    Print ( "dumb ... E")
Copy the code

The problem here is that when I enter 95, it prints the result is A, but 95 obviously also greater than the second condition elif score >=80:Yeah, why not print it B? This is because the code is judged from top to bottom, as long as one will not gone down, it must be clear ah!

Guess you like

Origin www.cnblogs.com/Rivend/p/11576122.html