博客
关于我
Python 之变量
阅读量:580 次
发布时间: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/

    你可能感兴趣的文章
    C语言_动态内存分配练习
    查看>>
    Linux学习_系统进程概念
    查看>>
    七层网络模型(待添加)
    查看>>
    考研复试——KY276 Problem C
    查看>>
    老鸟带你画tiled lines
    查看>>
    MybatisPlus自定义Sql实现多表查询
    查看>>
    Java位运算,负数的二进制表示形式,int类型最大值为什么是2的31次方-1
    查看>>
    WIFI模块开发教程之W600网络篇3:STA模式下TCP Client通信
    查看>>
    PyQt5快速上手基础篇10-QSettings用法
    查看>>
    JQuery--手风琴,留言板
    查看>>
    VUE框架应用包---------微信二维码应用
    查看>>
    MFC 自定义消息发送字符串
    查看>>
    goahead 下goaction测试与搭建
    查看>>
    Adding Powers
    查看>>
    不能将 "const char *" 类型的值分配到 "char *" 类型的实体
    查看>>
    ideal 下创建springboot项目
    查看>>
    Linux操作系统的安装与使用
    查看>>
    ajax请求出现/[object%20Object]错误的解决办法
    查看>>
    01背包(小偷的概率)
    查看>>
    流体运动估计光流算法研究
    查看>>