Download Lecture2-python_basics

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
1.---->>> print('Hello world!')
Hello world!
>>>
2.---->>>2+3
5
>>>
3.---->>> print('2+3=');2+3
2+3=
5
>>>
4.----Operator
Operation
Example Evaluates to...
**
Exponent
2 ** 3
8
%
Modulus/remainder
22 % 8
6
//
Integer division/floored quotient 22 // 8
2
/
Division
22 / 8
2.75
*
Multiplication
3*5
15
-
Subtraction
5-2
3
+
Addition
2+2
4
5.----->>> (5 - 1) * ((7 + 1) / (3 - 1))
16.0
>>>
6.---->>> 42+5+*2
SyntaxError: invalid syntax
>>>
7.----Data type
Examples
Integers
-2, -1, 0, 1, 2, 3, 4, 5
Floating-point numbers
-1.25, -1.0, --0.5, 0.0, 0.5, 1.0, 1.25
Strings
'a', 'aa', 'aaa', 'Hello!', '11 cats'
8.----- string concatenation
>>> 'Alice' + 'Bob'
'AliceBob'
>>>
9.---->>> 'Alice' + 42
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
'Alice' + 42
TypeError: Can't convert 'int' object to str implicitly
>>>
10.---->>> 'Alice' * 5
'AliceAliceAliceAliceAlice'
>>>
11.---->>> 'Alice' * 'Bob'
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
'Alice' * 'Bob'
TypeError: can't multiply sequence by non-int of type 'str'
>>>
12.---->>> 'Alice' * 5.0
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
'Alice' * 5.0
TypeError: can't multiply sequence by non-int of type 'float'
>>>
13.----- Storing Values in Variables
>>> spam = 40
>>> spam
40
>>>eggs = 2
>>> spam + eggs
42
>>>
14.---- overwriting the variable
>>> spam = 'Hello'
>>> spam
'Hello'
>>> spam = 'Goodbye'
>>> spam
'Goodbye'
>>>
15.---- Variable Names
Three rules:
1. It can be only one word.
2. It can use only letters, numbers, and the underscore (_) character.
3. It can’t begin with a number.
Valid variable names
Invalid variable names
balance
current-balance (hyphens are not allowed)
currentBalance
current balance (spaces are not allowed)
current_balance
4account (can’t begin with a number)
_spam
42 (can’t begin with a number)
SPAM
total_$um (special characters like $ are not allowed)
account4
'hello' (special characters like ' are not allowed)
Variable names are case-sensitive, meaning that spam, SPAM, Spam, and sPaM are four
different variables. It is a Python convention to start your variables with a lowercase letter.
16.----- Your First Program
# This program says hello and asks for my name.
print('Hello world!')
print('What is your name?') # ask for their name
myName = input()
print('It is good to meet you, ' + myName)
print('The length of your name is:')
print(len(myName))
print('What is your age?')
myAge = input()
print('You will be ' + str(int(myAge) + 1) + ' in a year.')
note: 1. Any text in a line following a hash mark (#) is a comment. Python ignores comments. 2.
print() function. 3. The input() function waits for the user to type some text on the keyboard and
press ENTER. 4. The len() function evaluates to the integer value of the number of characters in
that string.
17.---- The str(), int(), and float() Functions
>>> str(29)
'29'
>>> print('I am ' + str(29) + ' years old.')
I am 29 years old.
>>>
>>> int('42')
42
>>> int(1.25)
1
>>> float(10)
10.0
>>> int(-3.55)
-3
>>> str('3.14')
'3.14'
>>>
Note: the input() function always returns a string, even if the user enters a number.
>>>spam = input()
101
>>> spam
'101'
>>> spam=int(spam)
>>> spam
101
>>> spam * 10 / 5
202.0
>>>
Note: Division of two integers is a floating point.
>>> 11/8
1.375
>>>
Note: an integer can be equal to a floating point.
>>> 42 == '42'
False
>>> 42 == 42.0
True
>>> 42.0 == 0042.000
True
>>>
Python makes this distinction because strings are text, while integers and floats are both numbers.
>>> print('You will be '+str(int(myAge)+1)+' in a year')
You will be 5 in a year
>>> print('You will be '+str(int('4')+1)+' in a year')
You will be 5 in a year
>>> print('You will be '+str(4+1)+' in a year')
You will be 5 in a year
>>> print('You will be '+'5'+' in a year')
You will be 5 in a year
>>> print('You will be 5'+' in a year')
You will be 5 in a year
>>>
Related documents