標籤:官方 c語言 idt str 輸出 描述 format clu student
精通各種語言的 Hello World
#include <iostream>
int main(void)
{
std::cout<<"Hello world";
}
#include <stdio.h>
int main(void)
{
printf("\nhello world!"); \n 換行
return 0;
}
public class HelloWorld{
// 程式的入口
public static void main(String args[]){
// 向控制台輸出資訊
System.out.println("Hello World!");
}
}
<?php
echo "hello world!";
?>
puts "Hello world."
package main
import "fmt"
func main(){
fmt.Printf("Hello World!\n God Bless You!");
}
變數
電腦的主要作用之一:進行計算,用 Python進行資料運算非常容易,跟我們平常用電腦一樣簡單。
在算總消費的時候直接用的是之前已經算好的中間結果,這樣是為了避免重新再算一遍所有的資料。不正確的寫法:
>>> print(‘eat‘,10+15+7+4+7+3)
eat 46
>>> print(‘cloth‘,20)
cloth 20
>>> print(‘traffic‘,6+6+6+6+6)
traffic 30
>>> print(‘精神‘,300+300+400+200)
精神 1200
>>>
>>>
>>> print(‘總消費‘, 46+20+30+1200)
總消費 1296
這麼寫之所以是不正確,是因為最後算總消費的時候 是人肉 把之前算出來的分類結果 填進去的, 但是我們把程式寫在指令碼裡運行時, 電腦肯定不會預Crowdsourced Security Testing道吃飯、交通、買衣服3個分類的結果的,所以這個結果是我們自己動態算出來的,而不是電腦算出來的。
正確的寫法是,直接把每個分類結果先起個名字存下來,然後計算總消費的時候,只需要把之前存下來的幾個名字調用一下就可以了。
>>> eat = 10+15+7+4+7+3
>>> cloth = 20
>>> traffic = 6+6+6+6+6
>>> Spirit=300+300+200+400
>>>
>>> total = eat + cloth + traffic + Spirit
>>> print(‘總消費‘,total)
總消費 1296
- 變數:eat、cloth、traffic、Spirit、total 這幾個的作用就是把程式運算的中間結果臨時存到記憶體裡,可以讓後面的代碼繼續調用,這幾個的學名就叫做“變數”。
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.(變數用於儲存要在電腦程式中引用和操作的資訊。它們還提供了一種用描述性名稱標記資料的方式,這樣讀者和我們自己就能更清楚地理解我們的程式。將變數視為包含資訊的容器是有協助的。它們的唯一目的是在記憶體中標記和儲存資料。然後這些資料可以在整個程式中使用)。
name = "Alex Li"
變數名(標識符):name
變數值:Alex Li
①變數名只能是 字母、數字或底線的任意組合。
②變數名的第一個字元不能是數字。
③下面的關鍵字不能聲明為變數名:
‘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‘
①駝峰體
AgeOfOldboy = 56
NumberOfStudents = 80
②底線
age_of_oldboy = 56
number_of_students = 80
官方推薦使用第二種命名習慣,看起來更加的清晰。
①變數名為中文、拼音。
②變數名過長。
③變數名詞不達意。
age_of_oldboy = 56
oldboy_gf_name = ‘Lisa‘
>>>age_of_oldboy = 56 # 老男孩的年齡為56
>>>oldboy_gf_age = 54 #老男孩女朋友的年齡為54
>>>age_of_oldboy + oldboy_gf_age # 老男孩的年齡與老男孩女朋友的年齡為110
110 結果
>>>oldboy_gf_age = 55 #修改老男孩女朋友的年齡為55
>>>age_of_oldboy + oldboy_gf_age #修改後老男孩的年齡與老男孩女朋友的年齡為111
111 修改後的結果 常量
常量即指不變的量,例如,pai=3.141592653..., 或在程式運行過程中不會改變的量。在Python中沒有一個專門的文法代表常量,約定俗成用變數名全部大寫代表常量。例如:
AGE_OF_OLDBOY = 56
在c語言中有專門的常量定義文法,const int count = 60; 一旦定義為常量,更改即會報錯。
使用者互動
name = input("What is your name:")
print("Hello " + name )
執行指令碼後,發現程式會等輸入姓名後才能往下繼續走。
讓使用者輸入多個資訊:
name = input("What is your name:")
age = input("How old are you:")
hometown = input("Where is your hometown:")
print("Hello ",name , "your are ", age , "years old, you came from",hometown)
執行輸出:
What is your name:Wu qianqian
How old are you:21
Where is your hometown:ShiJiaZhuang
Hello Wu qianqian your are 21 years old, you came from ShiJiaZhuang
注釋
代碼注釋分為單行和多行注釋。 單行注釋用#,多行注釋可以用三對雙引號""" """。
例:
def subclass_exception(name, parents, module, attached_to=None):
"""
Create exception subclass. Used by ModelBase below.
If ‘attached_to‘ is supplied, the exception will be created in a way that
allows it to be pickled, assuming the returned exception class will be added
as an attribute to the ‘attached_to‘ class.
"""
class_dict = {‘__module__‘: module}
if attached_to is not None:
def __reduce__(self):
# Exceptions are special - they‘ve got state that isn‘t
# in self.__dict__. We assume it is all in self.args.
return (unpickle_inner_exception, (attached_to, name), self.args)
def __setstate__(self, args):
self.args = args
class_dict[‘__reduce__‘] = __reduce__
class_dict[‘__setstate__‘] = __setstate__
return type(name, parents, class_dict)
①不用全部加註釋,只需要在自己覺得重要或不好理解的部分加註釋即可。
②注釋可以用中文或英文,但絕對不要用拼音。
Python 變數與變數的作用、常量、使用者互動、注釋