1.Python is a dynamically strong type parsing language.
The first program. Print "hello world!"
>> print('Hello world')
>>Hello world
Variable
--------------------------------------------------------------------------------------------
A variable is a memory location where a programmer can store a value. Example : roll_no, amount, name etc.
Value is either string, numeric etc. Example : "Sara", 120, 25.36
Variables are created when first assigned.
Variables must be assigned before being referenced.
The value stored in a variable can be accessed or updated later.
No declaration required
The type (string, int, float etc.) of the variable is determined by Python
The interpreter allocates memory on the basis of the data type of a variable.
Variable Name Rules
----------------------------------------------------------------------------------------------
Must begin with a letter (a - z, A - B) or underscore (_)
Other characters can be letters, numbers or _
Case Sensitive
Can be any (reasonable) length
There are some reserved words which you cannot use as a variable name because Python uses them for other things.
Good Variable Name
----------------------------------------------------------------------------------------------
Choose meaningful name instead of short name. roll_no is better than rn.
Maintain the length of a variable name. Roll_no_of_a-student is too long?
Be consistent; roll_no or RollNo
Begin a variable name with an underscore(_) character for a special case.
-----------------------------------------------------------------------------------------------
The assignment statement creates new variables and gives them values.
Where the equal sign (=) is used to assign value (right side) to a variable name (left side). See the following statements :
>>> Item_name = "Oldman" #A String
>>> Item_qty = 10 #An Integer
>>> Item_value = 500.23 #A floating point
>>> print(Item_name)
Oldman
>>> print(Item_qty)
10
>>> print(Item_value)
500.23
>>>
---------------------------------------------------------------------------------------------
2.You can view the type of a variable using the type() function.
3.Python output format:
Variable references formatted output,% s string,%d can only accept Numbers,% f floating decimal
name = Vincent
info= "Name:%s" % (name)
print(info)
-------------------------------------------------------------------------------------------
4.You can input the password with ciphertext by using getpass which in getpass module.For example:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Author: Vincent Zhang
#using getpass module
import getpass
test = getpass.getpass("please input password:")
print(test)
---------------------------------------------------------------------------------------------
5.Conditional statements for python:if-else/while/for
if-else:
if username == user and password = pwd :
print("Welcome to login..")
else:
print("Your username or password is wrong!")
while:
count = 0
age = 28
while count <5:
guess_age = int(input("guess age"))
if guess_age == age:
print("you got it")
break
elif guess_age > age:
print("too bigger")
else:
print("too smaller")
count +=1
else:
print("you have try too many choice")
for:
age = 30
for i in range(5):
guess_age = int(input("guess age"))
if guess_age == age:
print("you got it")
break
elif guess_age > age:
print("too bigger")
else:
print("too smaller")
else:
print("you have try too many choice")
notice:for i in range(0,10,3) it means (0,3,6,9)
Continue/Break:Continue breaks out of the loop and enters the next loop. Break ends the loop
Example:
for i in range(10):
if i < 3:
print("loop",i)
else:
continue
print("Game Over.")
for i in range(10):
if i < 3:
print("loop",i)
else:
break
print("Game Over.")