博客
关于我
Python 之变量
阅读量:590 次
发布时间:2019-03-11

本文共 2301 字,大约阅读时间需要 7 分钟。

Python Variables Guide

Variables

In computer programs, a variable is a location (address) in memory that holds a value. Variables in Python are entities that store values and can be manipulated. When a variable is assigned a value, it is created. It's important to note that in Python, a string can only be concatenated with another string. If you need to concatenate other types of data, you must first convert them into string format using the str() function.

Key Points:

  • Each variable has a name and stores a value.
  • Variables in Python do not have a fixed type; the type is determined dynamically based on the value assigned.
  • The str() function is used to convert non-string data into a string format for concatenation.
  • Variable Naming

    When defining a variable, choose a name that clearly describes its purpose. Here are the rules for naming variables in Python:

  • The name must start with a letter or underscore and can include letters, numbers, and underscores thereafter.
  • Use meaningful names and avoid special characters like spaces, $, %, etc.
  • Variable names are case-sensitive.
  • Examples of poor naming:

    • variable1
    • my_var
    • info_1

    Examples of good naming:

    • temperature
    • current_time
    • user_score

    Constants

    Constants are variables that store fixed, immutable values. Unlike variables, the values of constants never change during program execution. (Note: Python does not have a built-in constant mechanism.)

    Key Point:

    • Constants are often used for readability and efficiency by storing fixed data in a single location.

    Data Types

    Data types determine the nature of data and how it is stored and manipulated. The main data types in Python are:

    • Integer (int): Represents whole numbers.
    • Float (float): Represents floating-point numbers.
    • Complex (complex): Represents complex numbers.

    Additional Points:

    • Python automatically determines the type of variable when it is assigned a value.
    • The str() function is used to convert numbers to strings during output.

    Input and Output

    The following code demonstrates how to read and print input in Python:

    a = input("Please enter an integer: ")print(a)
    • The input() function reads user input and returns a string.
    • The print() function outputs the string to the console.

    Examples:

  • print("Hello, World!") outputs Hello, World!
  • a, b = "x", "y" assigns a = "x" and b = "y"
  • 转载地址:http://bditz.baihongyu.com/

    你可能感兴趣的文章
    mysql 不区分大小写
    查看>>
    mysql 两列互转
    查看>>
    MySQL 中开启二进制日志(Binlog)
    查看>>
    MySQL 中文问题
    查看>>
    MySQL 中日志的面试题总结
    查看>>
    mysql 中的all,5分钟了解MySQL5.7中union all用法的黑科技
    查看>>
    MySQL 中的外键检查设置:SET FOREIGN_KEY_CHECKS = 1
    查看>>
    Mysql 中的日期时间字符串查询
    查看>>
    mysql 中索引的问题
    查看>>
    MySQL 中锁的面试题总结
    查看>>
    MySQL 中随机抽样:order by rand limit 的替代方案
    查看>>
    MySQL 为什么需要两阶段提交?
    查看>>
    mysql 为某个字段的值加前缀、去掉前缀
    查看>>
    mysql 主从
    查看>>
    mysql 主从 lock_mysql 主从同步权限mysql 行锁的实现
    查看>>
    mysql 主从互备份_mysql互为主从实战设置详解及自动化备份(Centos7.2)
    查看>>
    mysql 主从关系切换
    查看>>
    MYSQL 主从同步文档的大坑
    查看>>
    mysql 主键重复则覆盖_数据库主键不能重复
    查看>>
    Mysql 事务知识点与优化建议
    查看>>