print("Hello World!")
直接打印:Hello World!
可以直接用text文本写程序,完成之后,将后缀修改为 .py
# 是用来注释单行,注释多行使用 ''' ''',或者""" """
#!/usr/bin/env python env找环境变量
各种语言的Hello word!
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!";
?>
RUBY 语言
puts "Hello world!"
Go语言
package main
import "fmt"
func main(){
fmt.Printf("Hello World!\n God Bless You!");
}
课程主要使用pycharm
file--new project--location写名称--interpreter选择执行版本
创建目录 new directory
创建文件 python file--起名字
editor -- file and code templates 选择一个模板或新建模板
变量
name = "Alex Li" # name就是变量
name2 = name
print("My name is ", name, name2)
name = "PaoChe Ge"
gf_of_oldboy = "Chen rong hua"
#大写的变量名一般作为不可变的量赋值使用
print(name, name2)
变量名只能是字母,数字或下划线的任意组合
变量名的第一个字符不能是数字
关键字不要声明为变量如['and','if'……]
起变量名的时候请注意使用类似功能的英文单词
字符编码
ASCII码 最多用8位来表示,最多存255个
GB2312 存了7445个字符,1995年gbk收录了21886个字符,2000年GB18030收录了27484个字符
Unicode(统一码、万国码、单一码)
ASCII占8位1个字节,Unicode占16位2个字节
utf-8可变长度,utf-8存英文用1字节,存中文3字节
ASCII 255 1bytes
--> 1980 gb2312 7xxxx
-->1995 GBK1.0 2W+
-->2000 GB18030 27xxx
-->unicode 2bytes
-->utf-8 en:1bytes,zh:3bytes
py2 写入中文,需要加 # -*- coding:utf-8 -*-
py3 默认utf-8
用户输入
name = input("name:")
age= input("age:")
job= input("job:")
salary= input("salary:")
password = input("password:")
print(username,password)
info = '''
--------info of %s--------
Name:%s
Age:%s
Job:%s
Salary:%s
''' %(name, name,age,job,salary)
info2 = '''
--------info of {_name}--------
Name:_{name}
Age:{_age}
Job:{_job}
Salary:{_salary}
''' .format(_name=name,
_age=age,
_job=job,
_salary=salary)
info3 = '''
--------info of {0}--------
Name:{0}
Age:{1}
Job:{2}
Salary:{3}
''' .format(name,age,job,salary)
print(info3)
print(info)
%s s=string,%d 只能是数字 %f 浮点型,带小数点
age = int(input("age:")) 强制转换s为整型。input默认为s类型
import getpass #导入密文密码模块
_username = 'alex'
_password = 'abc123'
username = input("username:")
#password = getpass.getpass("password:") #pycharm 运行好像有问题
password = input("password:")
if _username == username and _password == password:
print("Welcome user {name} login...".format(name=username))
else:
print("invalid username or password!")
IndentationError(缩进错误)
新的程序 guess
age_of_oldboy = 56
guess_age = int(input("guess age:"))
if guess_age == age_of_oldboy:
print("yes, you got it.")
elif guess_age > age_of_oldboy:
print("think smaller...")
else:
print("think bigger!")
while循环
count = 0
while True:
print("count:", count)
count = count +1 #count+=1
if count == 1000:
break
改良的guess
age_of_oldboy = 56
guess_age = int(input("guess age:"))
count = 0
while count < 3:
if guess_age == age_of_oldboy:
print("yes, you got it.")
break
elif guess_age > age_of_oldboy:
print("think smaller...")
else:
print("think bigger!")
count += 1
else:
print("you have tried too many times..fuck off")
for 循环
for i in range(10):
print("loop", i)
for i in range(0, 10, 2): #2是步长,默认为1
print("loop", i)
for i in range(0, 10):
if i < 3:
print("loop", i)
else:
continue # 跳出本次循环进入下一次循环
print("hehe...")
for i in range(10):
print('------------', i)
for j in range(10):
print(j)
if j > 5:
break # 跳出本次循环
guess 改良
age_of_oldboy = 56
guess_age = int(input("guess age:"))
for i in range(3):
if guess_age == age_of_oldboy:
print("yes, you got it.")
break
elif guess_age > age_of_oldboy:
print("think smaller...")
else:
print("think bigger!")
else:
print("you have tried too many times..fuck off")
guess 改良
age_of_oldboy = 56
guess_age = int(input("guess age:"))
count = 0
while count < 3:
if guess_age == age_of_oldboy:
print("yes, you got it.")
break
elif guess_age > age_of_oldboy:
print("think smaller...")
else:
print("think bigger!")
count += 1
if count == 3:
countine_confirm = input("do you want to keep guessing..?")
if countine_confirm != 'n':
count = 0
else:
print("you have tried too many times..fuck off")