实验1 - python基础语法

实验目的:
通过python基础语法的学习,掌握python赋值语句和运算符、while循环语句和if语句的使用方法;为进一步使用python语言编程理解物联网安全中的经典加密算法、RSA加密算法等密码学算法,以及编写信息安全程序打下基础。

例1. 学会使用 while 语句用于循环执行程序:

# -*- coding: utf-8 -*-
"""
Created on Wed Apr 12 14:00:07 2017
@author: singing
"""

count = 0
while(count < 9):
    print 'The count is : '
    count = count + 1
print "Good bye!"

实验结果:

例2:学会使用if 语句用于控制程序的执行:

# -*- coding: utf-8 -*-
"""
Created on Wed Apr 12 14:07:03 2017

@author: singing
"""

num = 10
if num <0 or num > 10:
    print 'Hello'
else:
    print 'undefine'

if num > 0 and num <= 10:
    print 'hello'
else:
    print 'undefine'

if 0 < num <=10:
    print 'Helllo'
else:
    print 'undefine'

例3. 用While循环语句列出10以内的偶数:

# -*- coding: utf-8 -*-
"""
Created on Wed Apr 12 14:14:06 2017

@author: singing
"""

num = 0
while( num<10 ):
    if(num%2==0):
        print num
    num = num+1
print "the end"

实验结果:

例4、用While循环语句实现从1累加到100:

# -*- coding: utf-8 -*-
"""
Created on Wed Apr 12 14:15:10 2017

@author: singing
"""

num = 1
sum = 0
while(num<=100):
    sum = sum + num
    num=num+1   
print sum

实验结果:

你可能感兴趣的:(物联网安全)