zoukankan      html  css  js  c++  java
  • python输入输出及变量

    知识内容:

    1.python输出及注释

    2.变量及常量

    3.python用户输入

    4.Hello,World程序

    一、python输出及注释

    1.python输出

    在python2和python3中的输出均依靠print来实现,不过区别是python2中print为语句而在python3中print为内置函数

    python3中的print原型:  

      print(...)
      print(value, ..., sep=' ', end=' ', file=sys.stdout, flush=False)

      Prints the values to a stream, or to sys.stdout by default.
      Optional keyword arguments:
      file: a file-like object (stream); defaults to the current sys.stdout.
      sep: string inserted between values, default a space.
      end: string appended after the last value, default a newline.
      flush: whether to forcibly flush the stream.

    python输出: 

    1 print("Hello, Python!")      # 输出: Hello, Python!

    python中的参数:

      value是输出的值;sep是输出值之间的间距,默认为一个空格; end是一行后的最后输出,默认为 ,也就是说python的输出语句默认输出完后就换行;

      file将文本输入到file-like对象中,可以是文件,数据流等等,默认是sys.stdout;flush值为True或者False,表示是否立刻将输出语句输入到

      参数file指向的对象中(默认是sys.stdout),默认为Flase即只要将文件关闭文件中才有输入的数据,如果为true表示就算没有关闭文件数据也将被写入文件

    python的print参数示例:

    1 print("1", "2", "3", sep='sss')
    2 print("Hello", end=', ')
    3 print("Python", end=' This is end!')
    4 
    5 # 输出结果:
    6 # 1sss2sss3
    7 # Hello, Python This is end!

    2.python中的注释

    在程序开发中,一个项目多是由几个或几十个开发者一起做,你要调用别人写的代码,别人也要用你的,如果代码不加注释,自己都看不懂更不用说别人了,为了避免这种情况的发生,在程序中一定要求适当的注释

    单行注释: # 被注释的内容

    多行注释: ''' 被注释的内容 '''

    代码注释原则: 不用全部给代码加上注释,只需要在自己觉得重要或者不好理解的地方加上注释即可,注释可以用英文或中文,但是绝对不可以用拼音!

     1 # bulitins.py中的print函数
     2 def print(self, *args, sep=' ', end='
    ', file=None): # known special case of print
     3     """
     4     print(value, ..., sep=' ', end='
    ', file=sys.stdout, flush=False)
     5     
     6     Prints the values to a stream, or to sys.stdout by default.
     7     Optional keyword arguments:
     8     file:  a file-like object (stream); defaults to the current sys.stdout.
     9     sep:   string inserted between values, default a space.
    10     end:   string appended after the last value, default a newline.
    11     flush: whether to forcibly flush the stream.
    12     """
    13     pass

    二、变量及常量

    1.变量的定义

    官方解释:

    Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in memory. This data can then be used throughout your program.

    翻译:  变量用于存储在计算机程序中用于引用和操作的信息。它们还提供了一种用描述性名称对数据进行标记的方法,这样我们的程序就可以更清楚地被读者和我们自己理解。把变量看作容纳信息的容器是有帮助的。他们唯一的目的是在内存中标记和存储数据。然后可以在整个程序中使用这些数据。

     在python中不需要像C/C++那样提前声明变量名及其类型,直接赋值即可创建各种类型的变量

    1 x = 0           # 创建一个变量变量名为x并赋值为0
    2 x = "python"    # 创建一个变量变量名为x并赋值为python    

    2. 变量命名的规则

    (1)变量名只能是 字母、数字或下划线的任意组合

    (2)变量名的第一个字符不能是数字,只能以字母或下划线开头,但是最好命名变量时不要以下划线开头,因为以下划线开头的变量在Python中有特殊含义

    (3)以下关键字不能声明为变量名:

    ['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']

    (4)不建议使用系统内置的模块名、类型名或函数名以及已经导入的模块名和其中的成员名来作为变量名,这样可能会导致一些错误

    (5)变量名区分英文字母的大小写,例如username和Username是两个不同的变量

    (6)变量命名的方法有驼峰法和下划线分割法,如下所示

    驼峰法: StudentName、FirstDateTime

    下划线分割法: student_name、First_Date_Time

    python官方提倡使用下划线分割法

      注: 在python中采用的是基于值的内存管理方式,如果为不同变量赋值为相同的值,那么这个值在内存中只有一份,多个变量指向同一块内存地址; python具有内存管理功能,会跟踪所有的值,并自动删除不再有变量指向的值,因此在python中不需要考虑太多关于内存管理的问题 

    4.比较low的变量命名方式(不推荐使用!)

    (1)变量名为中文、拼音

    (2)变量名过长

    (3)变量名和意思不符

    1 姓名 = "wyb"
    2 xingming = "wyb"
    3 my_love_girl_name = "zl"
    4 name = "wyb"
    5 name = "湖北武汉"

    5.常量

    常量是指不变的量,如π = 3.141592623...,或者在程序运行过程中不会改变的量,python中没有一个专门的语法表示常量,程序员约定俗成变量名全部大写表示常量

    1 MY_AGE = 21

    三、python用户输入

    1. python3中的用户输入

    在python3中用户输入依靠input函数实现,python3input() 函数接受一个标准输入数据,返回为 string 类型。

    函数语法:  input([prompt])   # prompt为提示信息,一般是字符串形式

    示例:

    type()函数在python中是用来返回对象的类型,对象是python中最基本的概念之一,在python中的一切都是对象。python中有很多内置对象供我们使用,比如说数字、字符串、列表、元组、字典、集合、del命令以及type()、print()等大量内置函数。

    注: python3 里input()接收所有输入,并默认将所有的输入都看作字符串来处理,返回字符串类型

    2. python2中的用户输入

    在python2中用户输入依靠input()和raw_input()实现,input() 相等于 eval(raw_input(prompt)) ,用来获取控制台的输入。

    raw_input() 将所有输入作为字符串看待,返回字符串类型。而 input() 在对待纯数字输入时具有自己的特性,它返回所输入的数字的类型( int, float ),input()处理字符串时输入的字符串必须用引号括起来,否则会引发一个 SyntaxError错误

    input()示例:

    raw_input()示例:

    注意:

    input() 和 raw_input() 这两个函数均能接收字符串 ,但 raw_input() 直接读取控制台的输入(任何类型的输入它都可以接收)。而对于 input() ,它希望能够读取一个合法的 python 表达式,即输入字符串的时候必须使用引号将它括起来,否则会引发一个 SyntaxError 。

    在python2中除非对 input() 有特别需要,否则一般情况下我们都是推荐使用 raw_input() 来与用户交互。在python3中就不用纠结,直接使用input()

    四、Hello,World程序

    1.python的Hello,World程序文件执行

    (1)在桌面保存一个hello.py文件,并写入以下代码:

    1 print("Hello, World")        # 输出Hello, World

    (2)在命令行中进入桌面的路径,敲入以下命令运行程序:

    python3是指定了python3.exe来解释执行hello.py文件

    2.python的Hello,World程序交互式执行

    除了上面的那样把Hello,World程序写入一个文件然后来执行外,也可以使用python的交互式环境来执行Hello,World程序

    下面演示了python3中的Hello,World程序交互式执行和python2中的Hello,World程序交互式执行:

    3.其他经典语言的Hello, World程序

    (1)C语言:

    1 #include <stdio.h>
    2 int main(void)
    3 {
    4     printf("hello world!
    ");
    5     return 0;
    6 }

    (2)C++:

    1 #include <iostream>
    2 using namespace std;
    3 int main(void)
    4 {
    5     cout<<"Hello world" << endl;
    6 }

    (3)C#:

    1 public class HelloWorld
    2 {
    3     public static void Main()
    4     {
    5         System.Console.WriteLine("HELLO WORLD");
    6     }
    7 }

    (4)Java:

    1 public class HelloWorld{
    2   // 程序的入口
    3   public static void main(String args[]){
    4     // 向控制台输出信息
    5     System.out.println("Hello World!");
    6   }
    7 }

    (5)JavaScript:

    1 console.log("Hello, World!")   //在浏览器的console中输出Hello, World!

    (6)PHP:

    1 <?php  
    2           echo "hello world!";  
    3 ?> 

    (7)Ruby:

    1 puts "Hello world."  

    (8)Go:

     1 package main
     2 
     3 import "fmt"
     4 
     5 func main(){
     6 
     7     fmt.Printf("Hello World!
    ");
     8 
     9 }
    10 
    11 Go
    View Code

     附:

    1.用户交互:

     1 # __author__ = "wyb"
     2 # date: 2018/3/3
     3 
     4 death_age = 80
     5 
     6 # input 接受的所有数据都是字符串,即便你输入的是数字,但依然会被当成字符串来处理
     7 name = input("your name: ")
     8 age = input("your age: ")
     9 print(type(name))           # type() -> 输出对象的类型
    10 print(type(age))
    11 print("Your name:", name)
    12 
    13 # int() -> 转换成整数    str() -> 转换成字符串类型
    14 # 以下两种方法输出结果一样,但是本质不一样:
    15 # 字符串直接输出:
    16 print("You can still live for ",  death_age - int(age), " years ....")
    17 # 字符串拼接:
    18 print("You can still live for " + str(death_age - int(age)) + " years ....")
    View Code

    2.自然数取位:

     1 # __author__ = "wyb"
     2 # date: 2018/3/10
     3 
     4 # 用户输入一个三位自然数,计算并输出其佰位、十位和个位上的数字
     5 
     6 # 方法1:
     7 # x = input('请输入一个三位数:')
     8 # x = int(x)
     9 # a = x // 100          # 取百位
    10 # b = x // 10 % 10      # 取十位
    11 # c = x % 10            # 取个位
    12 # print(a, b, c)
    13 
    14 # 方法2:
    15 # x = input('请输入一个三位数:')
    16 # x = int(x)
    17 # a, b = divmod(x, 100)   # 取百位和百位后的数
    18 # b, c = divmod(b, 10)    # 取十位和个位
    19 # print(a, b, c)
    20 
    21 # 方法3:
    22 # x = input('请输入一个三位数:')
    23 # a = int(x[0])       # 取百位
    24 # b = int(x[1])       # 取十位
    25 # c = int(x[2])       # 取个位
    26 # print(a, b, c)
    27 
    28 # 方法4:
    29 x = input('请输入一个三位数:')
    30 a, b, c = map(int, x)
    31 print(a, b, c)
    View Code

     

  • 相关阅读:
    CentOS 7.4 如何安装 MariaDB 10.3.9 Stable 数据库
    xxx is not in the sudoers file. This incident will be reported.
    CentOS 7.4 上如何安装 tomcat 9
    CentOS 7.4 下面安装 jdk 10 的一点总结
    CentOS 7.4 下安装 Nginx
    MySQL数据库常用操作
    chart学习
    Ext需要的文件目录
    获取浏览器信息
    运行容器
  • 原文地址:https://www.cnblogs.com/wyb666/p/8503595.html
Copyright © 2011-2022 走看看