程序代写代做代考 python Java PowerPoint Presentation

PowerPoint Presentation

Basics in programming

Lectures 4
Recap of Python IDE
Number Representations
Arithmetic Operations
Working with Strings
Decission making
Input and Output

Number formats
In a computer, everything is stored as a sequence of 0’s and 1’s. This is called as binary format.

For example, the string 01010011 can be interpreted as the decimal number 83.

It can also represent the capital letter ’S’.

How we interpret these strings of 0’s and 1’s is up to us.

Number formats
We can tell the CPU how to interpret a location in memory by which instruction we tell the CPU to execute.

Some instructions treat 01010011 as the number 83. Other instructions treat it as the letter ‘S’

Number formats
One digit in a binary number (f.ex 01010011 )is called a bit.

Eight bits grouped together are called a byte.

Four bytes grouped together are called a word.

Number formats
2^10 bytes are called a kilobyte (i.e. KB).

2^10 kilobytes are called a megabyte (i.e. MB).

2^10 megabytes are called a gigabyte (i.e. GB).

2^10 gigabytes are called a terabyte (i.e. TB).

Binary Number Representation
Each digit in a decimal number represents a power of 10. The right-most digit is the number of ones, the next digit is the number of 10’s, and so on.

To interpret integers as binary numbers we use powers of 2 just as we use powers of 10 when interpreting integers as decimal numbers.

Binary Number Representation
The right-most digit of a binary number represents the number of times 2^0 = 1 is needed in the representation of the integer.

Our choices are only 0 or 1 (i.e. we can use one 20 if the number is odd), because 0 and 1 are the only choices for digits in a binary number

Binary Number Representation
The next right-most is 2^1 = 2 and so on.

So 01010011 is 0 ∗ 2^7 + 1 ∗ 2^6 + 0 ∗ 2^5 +1 ∗ 2^4 +0 ∗ 2^3 +0 ∗ 2^2 +1 ∗ 2^1 +1 ∗ 2^0 = 83

https://www.mathsisfun.com/binary-number-system.html

Binary Number Representation
Most programmers do not have to work with binary number representations.

Programming languages let programmers write numbers in base 10 and they do the conversion for us.

Hexadecimal and Ocatal Number Representation
But sometimes its needed, so for that we have adopted two other representations for binary numbers:
base 16 (called hexadecimal)
base 8 (called octal).

In hexadecimal each digit of a number can represent sixteen different binary numbers.

The sixteen hexadecimal digits are 0–9, and A–F. Since 16 is a power of 2, there are exactly 4 binary digits that make up each hexadecimal digit.

So, 00002 is 016 and 11112 is F16. So, the binary number 10101110 is AE in hexadecimal notation and 256 in octal notation.

Hexadecimal and Ocatal Number Representation

If we wish to convert either of these two numbers to binary format the conversion is just as easy. 10102 is A16 for instance.

Again, these conversions can be done quickly because there are four binary digits in each hexadecimal digit and three binary digits in each octal digit.
Hexadecimal and Ocatal Number Representation

Example

To convert the binary number 010100112 to hexadecimal we have only to break the number into two four digit binary numbers 01012 and 00112.

01012 = 516 and 00112 = 316. So the hexadecimal representation of 010100112 is 5316.
Hexadecimal and Ocatal Number Representation

Since 8 = 23, each digit of an octal number represents three binary digits.

The octal digits are 0–7. The number 010100112 = 1238.

When converting a binary number to octal or hexadecimal we must be sure to start with the right-most bits. Since there are only 8 bits in 01010011 the left-most octal digit corresponds to the left-most two binary digits. The other two octal digits each have three binary digits.
Hexadecimal and Ocatal Number Representation

https://www.mathsisfun.com/hexadecimals.html

How does this relate to Python?

Python has built-in support of hexadecimal numbers. If you want to express a number in hexadecimal form you preface it with a 0x

Try creating variable with content of ”0x53” and print it.

Also try ”0o123”
How does this relate to Python?

We have learned that bytes in memory can be interpreted in different ways.

The way bytes in memory are interpreted is determined by the type of the value or object and the operations we apply to these values.
Data types and operations

Each value in Python is called an object. Each object is of a particular type.

There are several data types in Python.

These include basic types:
-integer (called int in Python)
-float, boolean (called bool in Python)
-string (called str in Python)
Data types and operations

How does this relate to Python?
/ object
Let’s make small program with few variables (10 of them)

Arithmetic operations

the type of the result is a float if either operand is a float unless noted otherwise in the table.

Dividing the integer 83 by 2 yields 41.5 if it is written 81/2.

However, if it is written 83//2 then the result is 41.

This goes back to long division as we first learned in elementary school. 83//2 is 41 with a remainder of 1. The result of floor division isn’t always an int. 83//2.0 yields 41.0 so be careful. While floor division returns an integer, it doesn’t necessarily return an int.

Arithmetic operations

Practice some arithmetic opeartions,

Do one of each and print their results
+

*
/
//
%
**

Arithmetic operations

Test some mathematical functions:
float
Int
abs
round

Arithmetic operations

String is just another datatype in Python:

name = ’Jari’
print(”Hi my name is “, name)

This is initializes a variable called name to the string ‘Jari’. A string literal is an actual string value written in your program. String literals are delimited by either double or single quotes. Delimited means that they start and end with quotes.
Delimiters must come in matching pairs
Working with strings

Strings are one type of sequence in Python.
Python supports operations on sequences. For instance, you can get an individual item from a sequence.

print(name[0])

will print the first character of the string that name references
Working with strings

You can add strings together to build a new string. So,

name = ”Firstname” + ” ” + ”Lastname”

Will make varianle name to contain:

”Firstname Lastname”
Working with strings

It is possible in Python to convert an integer to a string. For instance:

x = str(83)
print(x[0])
print(x[1])
y = int(x)
print(y)

This program converts 83 to ‘83’ and back again.

Working with strings

Conversion of numeric values to strings should not be confused with ASCII conversion. Integers may represent ASCII codes for characters.

If you want to convert an integer to its ASCII character equivalent you use the chr conversion operator.

For instance, chr(83) is ‘S’. Likewise, if you want to convert a character to its ASCII code equivalent you use the ord conversion operator. So ord(‘S’) is equal to 83.
Working with strings

Working with strings

Test few string operations:

indexing
concatenation
chr
str
int
float

Working with strings

Working with strings

If Statement
Decission making

If Statement

Python does not use { } to enclose blocks of code for if/loops/function etc..
Instead, Python uses the colon (:) and indentation/whitespace to group statements.

The boolean test for an if does not need to be in parenthesis (big difference from C++/Java), and it can have *elif* and *else* clauses (mnemonic: the word “elif” is the same length as the word “else”).
Decission making

There is also a Boolean type with two values: True and False (converted to an int, these are 1 and 0).

Python has the usual comparison operations: ==, !=, <, <=, >, >=. Unlike Java and C, == is overloaded to work correctly with strings.
Decission making

Decission making

The boolean operators are the spelled out words *and*, *or*, *not* (Python does not use the C-style && || !).

Decission making

Decission making

Sometimes, you may want your program to do one thing if a condition is true and something else if a condition is false.

Notice that the if statement does something only when the condition evaluates to true and does not do anything otherwise.

If you want one thing to happen when a condition is true and another to happen if the condition is false then you need to use an if-else statement.
Decission making

Decission making

Again, indentation is very important. The else keyword must line up with the if statement to be properly paired with the if statement by Python.

Decission making

The need to select between several choices presents itself often enough that Python has a special form of the if statement to handle this.

It is the if-elif statement. In this statement, one, and only one, alternative is chosen.

The first alternative whose condition evaluates to True is the code that will be executed. All other alternatives are ignored.
Decission making

Decission making

Getting input
To get input from the user you can use the input function.

When the input function is called the program stops running the program, prompts the user to enter something at the keyboard by printing a string called the prompt to the screen, and then waits for the user to press the Enter key

Getting input

The type of value read by Python is always a string

Getting input
Input can be converted to number by using int()-function for example:

age = int(input(“Please enter your age:”))
olderAge = age + 1 print(“Next year you will be”, olderAge)

Formatting output
When printing, we may print as many items as we like on one line by separating each item by a comma.

Each time a comma appears between items in a print statement, a space appears in the output.

name = ”Jari”
print(“I hope that,”, name, “is feeling well today.”)

Formatting output
To print the contents of variables without spaces appearing between the items, the variables must be converted to strings and string concatenation can be used.

The + operator adds numbers together, but it also concatenates strings.

For the correct + operator to be called, each item must first be converted to a string before concatenation can be performed.

Formatting output
When printing, we may print as many items as we like on one line by separating each item by a comma.

Each time a comma appears between items in a print statement, a space appears in the output.

num1 = 5
num2 = 10
print(str(num1)+”*”+str(num2)+”=”+str(num1+num2))

String %

The % operator takes a printf-type format string on the left (%d int, %s string, %f/%g floating point), and the matching values in a tuple on the right (a tuple is made of values separated by commas, typically grouped inside parentheses)
Formatting output

# % operator
text = “%d little pigs come out, or I’ll %s, and I’ll %s, and I’ll blow your %s down.” % (3, ‘huff’, ‘puff’, ‘house’)
Formatting output

Formatting output

Excercises 3.
Write a program that converts centimeters to yards, feet, and inches. There are 2.54 centimeters in an inch.
Centimeters are given in variable at the start of the program

Write application that finds out max of three interegers. These integers are given as variables at start of the program. Print maximum number in binary, octal and hexadecimal format.

/docProps/thumbnail.jpeg