python基础

第一个python程序

文件执行

  1. 用notepad++创建一个文件,输入以下代码
  2. print("Hello World!")
  3. 保存为HelloWorld.py , 注意要强调.py后缀名的作用
  4. 进入cmd命令行,执行python HelloWorld.py, 看结果 (注意要解释文件名前面加python 的原因是要把代码交给python解释器去解释执行)

交互器执行

  用python交互器输出helloword!

各种语言的Hello World

 c++    

#include <iostream>
 int main(void)
 {
  std::cout<<"Hello world";
 }

 c

 #include <stdio.h>

int main(void)
{
printf("\nhello world!");
return 0;
}

 

 java

 public class HelloWorld{

  // 程序的入口
  public static void main(String args[]){
    // 向控制台输出信息
    System.out.println("Hello World!");
  }
}

 PHP

 

<?php  
             echo "hello world!";  
?>

 

 GO

package main

import "fmt"

func main(){

    fmt.Printf("Hello World!\n God Bless You!");

}

python
print(“helloword”)

 

 


 

 

变量

 

什么是变量

  把程序运算的中间结果临时存到内存里,以备后面的代码继续调用,这几个名字的学名就叫做“变量”。

 

变量的作用

  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.

 

变量定义规则

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

  2、变量名的第一个字符不能是数字。

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

 

常量

  在Python中没有一个专门的语法代表常量,程序员约定俗成用变量名全部大写代表常量。

 

 


 

 

 

程序交互

 

读取用户输入

  

name = input("What is your name?")

print("Hello " + name)

 

 

注释

  代码注释分单行和多行注释, 单行注释用#,多行注释可以用三对双引号""" """。

 

posted @ 2018-08-21 15:41  royal-C  阅读(146)  评论(1)    收藏  举报