zoukankan      html  css  js  c++  java
  • python1-变量和简单的数据类型

    变量和简单的数据类型

    1 Hello World程序

    1.1 执行py文件

    linux下编辑一个文件,hello.py

    1 print("Hello world")

    执行

    1 # python hello.py

    1.2 指定交互器

    变成可执行程序,第一句要声明解释器

    1 #!/usr/bin/env python
    2 print("Hello world")
    3 加执行权限
    4 # chmod 755 hello.py
    5 执行
    6 # ./hello.py

    1.3 交互器中执行

    1 MacBook-Pro:Tools yjn$ python3
    2 Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55)
    3 [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
    4 Type "help", "copyright", "credits" or "license" for more information.
    5 >>> print("Hello world")
    6 Hello world
    7 >>>

    2 变量

    2.1 变量定义规则

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

    l  变量的第一个字符不能是数字

    l  关键字不能生命为变量名:

    2.2 变量的赋值

    代码:

    1 # Author:Yang Jianan
    2 
    3 name = "YJN"
    4 name2 = name
    5 print("My name is",name,name2)
    6 name = "jianan"

    结果:

    1 My name is YJN YJN
    2 jianan YJN

    3 字符编码

    3.1 发展史

    • python解释器在加载.py文件中的代码时,会对内容进行编码(默认ascill)
    • ASCII基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧语言,其最多只能用8位表示(一个字节),即2**8=256-1,所以,ASCCI,最多只能表示255个符号。
    • ASCII --》255 1bytes
    • 1980年gb2312 ---》7XXX
    • 1995年GBK1.0 --》2W+
    • 2000年GB18030 --》 27XXX
    • unicode --》 2bytes
    • utf8 --》 en:1bytes,zh:3bytes

    3.2 python23区别

    python3 默认中文字符集utf8

    python2 写中文需要声明字符集

    1 # -*- coding:utf-8 -*-

    4 '''用法

    4.1 注释多行

    1 '''
    2 多行内容
    3 '''

    4.2 打印多行

    代码:

    1 msg='''
    2 name = "YJN"
    3 name2 = name
    4 print("My name is",name,name2)
    5 name = "jianan"
    6 print(name,name2)
    7 '''
    8 print(msg)

    结果:

    1 name = "YJN"
    2 name2 = name
    3 print("My name is",name,name2)
    4 name = "jianan"
    5 print(name,name2)

    5 简单数据类型

    5.1 字符串

    在pyton中,用引号括起来的都是字符串,引号是单引号或者双引号。

    等于:

    name = "YJN"

    name = 'YJN'

    区分:

    name = "I'm YJN"

    5.1.1   使用方法修改字符串的大小写

    1 >>> name = "yang jianan"
    2 >>> print(name.title())  # 首字母大写
    3 Yang Jianan
    4 >>> name = "Yang Jianan"
    5 >>> print(name.upper()) # 所有字母大写
    6 YANG JIANAN
    7 >>> print(name.lower())  # 所有字母小写
    8 yang jianan

    5.1.2   合并(拼接)字符串

    1 >>> first_name = "yang"
    2 >>> last_name = "jianan"
    3 >>> full_name = first_name + " " + last_name
    4 >>> message = "Hello," + full_name.title() + "!"
    5 >>> print(message)
    6 Hello,Yang Jianan!

    5.1.3   使用制表符或者换行符添加空白

    制表符:

    换行符:

    1 >>> print("language:
    	python
    	java
    	c")
    2 language:
    3    python
    4    java
    5    c

    5.1.4   删除空白

     1 >>> beauty = " yjn "
     2 >>> beauty.rstrip() # 删除结尾空白
     3 ' yjn'
     4 >>> beauty.lstrip() # 删除开头空白
     5 'yjn '
     6 >>> beauty.strip()  # 删除开头和结尾空白
     7 'yjn'
     8 >>> beauty # 这种删除是暂时的,再次访问还有空白
     9 ' yjn '
    10 >>> beauty = beauty.strip() # 删除结果存到变量,再次访问无空白
    11 >>> beauty
    12 'yjn'

    5.2 数字

    整数

    浮点数

    运算符号:+ - * / ()

    5.2.1   使用str()避免类型错误

    在字符串中使用整数时,需要转换为字符串,否则python会将它看成整数类型。

    1 >>> age = 18
    2 >>> message = "Happy " + age + "rd birthday!"
    3 Traceback (most recent call last):
    4   File "<stdin>", line 1, in <module>
    5 TypeError: must be str, not int
    6 >>> message = "Happy " + str(age) + "rd birthday!"
    7 >>> print(message)
    8 Happy 18rd birthday!
  • 相关阅读:
    晶振故障原因
    国外被疯赞的一篇神文:你该增加人生技能了(转)
    Python学习笔记-Day27-正则表达式
    Python学习笔记-Day25-模块(hashlib、configpaser,logging)
    Python学习笔记-Day24-collections模块
    Python学习笔记-Day23-模块(时间、随机数、sys、os)
    Python学习笔记-Day22-内置方法及序列化模块
    Python学习笔记-Day21-反射、__str__、__repr__
    @classmethod 与 @staticmethod
    @property(setter、deleter)将类中的方法伪装成属性
  • 原文地址:https://www.cnblogs.com/yangjianan/p/8860395.html
Copyright © 2011-2022 走看看