Back to 课程

Computer Science GCES OCR

0% Complete
0/0 Steps
  1. Cpu Architecture Performance And Embedded Systems Ocr
    5 主题
  2. Primary And Secondary Storage Ocr
    6 主题
  3. Data Storage And Compression Ocr
    12 主题
  4. Networks And Topologies Ocr
    6 主题
  5. Wired And Wireless Networks Protocols And Layers Ocr
    6 主题
  6. Identifying And Preventing Threats To Computer Systems And Networks Ocr
    2 主题
  7. Operating Systems And Utility Software Ocr
    2 主题
  8. Ethical Legal Cultural And Environmental Impact Ocr
    2 主题
  9. Computational Thinking Searching And Sorting Algorithms Ocr
    3 主题
  10. Designing Creating And Refining Algorithms Ocr
    5 主题
  11. Programming Fundamentals And Data Types Ocr
    5 主题
  12. Additional Programming Techniques Ocr
    7 主题
  13. Defensive Design And Testing Ocr
    6 主题
  14. Boolean Logic Diagrams Ocr
    2 主题
  15. Programming Languages And Integrated Development Environments Ides Ocr
    3 主题
  16. The Exam Papers Ocr
    2 主题
  17. Structuring Your Responses Ocr
    3 主题
课 Progress
0% Complete

Exam code:J277

Primitive Data Types

What is a data type?

  • A data type is a classification of data into groups according to the kind of data they represent

  • Computers use different data types to represent different types of data in a program

  • The basic data types include:

Data type

Used for

Example

Integer

Whole numbers

10, -5, 0

Real

Numbers with a fractional part

3.14, -2.5, 0.0

Character 

Single character

‘a’, ‘B’, ‘6’, ‘£’

String

Sequence of characters

“Hello world”, “ABC”, “@#!%”

Boolean

True or false values

True, False

  • It is important to choose the correct data type for a given situation to ensure accuracy and efficiency in the program

  • Data types can be changed within a program, this is called casting

What is casting?

  • Casting is when you convert one data type to another data type

Example

  • The following Python program is used to capture a users age to determine if they are old enough to vote

Line

Python code

01

age = input(“Enter age”)

02

if age >= 18:

03

print(“Old enough to vote”)

04

else:

05

print(“Too young to vote”)

  • In this example, on line 01, no specific data type is requested

  • By default the data type is stored as ‘string’

  • On line 02, a run-time error would occur because age is stored as a string and is being compared to an integer value in the selection statement

  • Casting the age from a string to an integer would solve the error

Line

Python code

01

age = input(“Enter age”)

02

if int(age) >= 18:

03

print(“Old enough to vote”)

04

else:

05

print(“Too young to vote”)

  • In the corrected code, casting is highlighted in green

Casting between data types

Conversion

Example

Output

From Integer to Real

int_value = 5 real_value = float(int_value)

5.0

From Real to Integer

real_value = 5.7 int_value = int(real_value)

5

From String to Integer

str_value = "10" int_value = int(str_value)

10

From Integer to String

int_value = 5 str_value = str(int_value)

“5”

From Boolean to String

bool_val = True str_val = str(bool_val)

“True”

From String to Boolean

str_value = "True" bool_val = bool(str_value)

True

Worked Example

Customers booking a holiday can choose between half board or all inclusive and a hotel star rating between 1 and 5

A typical booking record is shown in the table:

firstName

Jacob

lastName

Franks

boardType

All inclusive

starRating

5

bookingComplete

True

 State the most appropriate data type for the following fields [2]

boardType

 

starRating

 

 Give the name of one field that could be stored as a Boolean data type [1]

Answer

boardType

String

starRating

Integer

  • bookingComplete

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注