1.Python 介绍
2.发展史、python3安装(自己搜)
3.简单程序
4.变量
5.用户输入
6.模块介绍
7.数据类型
8.数据运算
9.if....else/for/while
3.第一个简单程序
Linux下创建一个文件helloworld.py
print("Hello World!")
windows 中执行 python helloworld
如果类似执行一个shell脚本,如./helloworld.py,需要在helloworld.py文件中加入一个指定解释器如下:
#!/usr/bin/env python
print("Hello World!")
执行./helloworld.py。
At:执行前需要给于helloworld执行权限,chmod 755helloworld.py
C++
#include<iostream>
int main(void){
std: :cout<<"Hello World!";
}
c
#include<stdio.h>
int main(void)
{
printf("Hello World!");
return 0;
}
java
public class HelloWorld{
public static void main(String args[]){
System.out.println("Hello World!");
}
}
PHP
<?php
ecoh "Hello World!";
>
Ruby
puts"hello world!"
go
package main
import "fmt"
func main(){
fmt.Printf("Hello world!\n God love you")
}
4.变量、字符编码
#使用Pycharm开发,python3.
模板设计:Templates.py
#!/usr/bin/env python # -*- coding:utf-8 -*-
第一句最好带着,方便以后跨平台移植:第二句py2最好带上,py3可以不带,3 Unicode统一编码。
变量:var.py
name = "zhang" print("My name is",name)
name = "zhangguoqi" name2 = name #name2指向name内容,不会随着以后name的改变而改变 print(name,name) name = "ZGQ" print(name2,name)
变量的定义规则
*字母、数字、下划线或任意组合
*第一个字符不能是数字
*关键字不能作为变量如:and/as/assert/break/class/continue/def/del/elif/else/except/exec/finally/for/form/global/if
字符串的拼接:
name = input("name:") age = input("age:") job = input("job:") salary= input("salary:") # 字符串的拼接1 info = ''' ------ info of '''+ name +'''------ Name:'''+ name +''' Age:'''+age + ''' Job:'''+job+''' Salary:'''+salary+''' ''' print(info) #字符串的拼接2 info2 = ''' ------ info of %s------ Name:%s Age:%s Job:%s Salary:%s ''' %(name,name,age,job,salary) print(info2) #第三种字符串拼接3 info3= ''' ------ info of {_name}------ Name:{_name} Age:{_age} Job:{_job} Salary:{_salary} '''.format(_name=name, _age=age, _job=job, _salary=salary) print(info3) # 第四种拼接4 info4= ''' ------ info of {0}------ Name:{0} Age:{1} Job:{2} Salary:{3} '''.format(name,age,job,salary) print(info4)
字符编码与二进制
字符编码:根据01 ——ASCII码,中文字符编码gbk,py3 统一的万国吗utf-8。
#!/usr/bin/env python ,#-*-coding: utf-8-*- ,
ASCII 255个字符,1个字节8个bite;gbk 18030字符(增加中),3个字节24个bite;
python3直接默认utf-8。
注释:单行#;多行''' '''; 注意:shell中区别单双引号;
7用户输入
#name = "你好,世界" name = "zhang" print("My name is",name)
输入密码时,如果不想被看见,需要利用getpass模块;
注意pycharm中不好使。
判断用户名对不对( if else)
elif
age_of_QQ = 26 guess_age = int(input("guess_age:")) if guess_age == age_of_QQ: print(" well! you got it") elif guess_age > age_of_QQ: print("you think is bigger") else: print("you think is smaller")
while判断
age_0f_oldboy = 26 count = 0 while count<3: guess_age = int(input("guess_age:")) if guess_age == age_0f_oldboy: print("yes,you got it") break elif guess_age > age_0f_oldboy: print("think smaller...") else: print("thing bigger!") count+=1 else: print("you have trined mang times.fark off!")
for语句
count = 0 ''' while True: print("count:",count) count = count +1 #count+=1 if count == 1000: break ''' '''for i in range(0,10,2): #补偿分割 print("loop",i)''' for i in range(10): print("-----",i) for j in range(10): print(j) if j>5: break
guess for
age_0f_oldboy = 26 count = 0 for i in range(3): guess_age = int(input("guess_age:")) if guess_age == age_0f_oldboy: print("yes,you got it") break elif guess_age > age_0f_oldboy: print("think smaller...") else: print("thing bigger!") count+=1 if count == 3: continue_confirm = input("do you want to keep guessing..?") if continue_confirm !='n': count =0 else: print("you have trined mang times.fark off!")