MCS-201 · Unit 10

Data Structures and Control Statements in Python

Section 4 of 12

Block 3 · Unit 10Data Structures and Control Statements in Python8 min · Section 4 of 12

10.4Numbers, Strings and None

Covers IGNOU MCS-201 §10.7, 10.7.1, 10.7.2, 10.7.3

A data type tells Python what kind of value an object is and what operations it supports. This section covers the basic, single-value types; the collection types follow in the next sections.

Numeric types

TypeMeaningExamples
intWhole numbers of unlimited size-12, 0, 123, 10**20
floatReal numbers, stored as 64-bit binary floating point (like C’s double)-2.04, 4.0, 1.5e3
complexComplex numbers; j is the imaginary unit3+4j, 2-2j
boolThe truth values True and False (a subtype of int: True behaves as 1)True, False, 10 > 7

Unlike C, Python integers never overflow. Floats, however, are approximate: 0.1 + 0.2 gives 0.30000000000000004.

type(x) shows a value’s type, and isinstance(x, int) tests it.

Type conversion

FunctionExampleResult
int()int("42"), int(3.99)42, 3 (cuts off the fraction)
float()float("2.5"), float(3)2.5, 3.0
str()str(31)"31"
bool()bool(0), bool("hi")False, True

Converting with these functions is called type conversion or type casting; Python also converts automatically in mixed arithmetic, for example 3 + 2.5 gives the float 5.5.

Strings

  • 'hello' and "hello" are the same; triple quotes """...""" allow several lines.
  • There is no separate char type: a character is a string of length 1.
  • len(s) gives the length.
  • Indexing: s[0] is the first character; negative indexes count from the end, so s[-1] is the last.
  • Slicing: s[start:stop] gives the characters from start up to but not including stop; omitted start means 0, omitted stop means the end; s[::-1] reverses.
  • + joins strings, * repeats them.
  • Strings are immutable: s[0] = 'h' raises TypeError; create a new string instead.
Indexes of 'Hello Friend'
 H  e  l  l  o     F  r  i  e  n  d
 0  1  2  3  4  5  6  7  8  9 10 11
-12            ...              -2 -1

None

None is a special value of type NoneType meaning “no value”. It is what a function returns when it has no return statement, and is often used as a placeholder: money = None. Test for it with is: if money is None:.

Key takeaways

  • Numbers: int (no overflow), float (approximate), complex, bool.
  • Strings are immutable sequences; index from 0 or from −1 at the end; slices exclude the stop index.
  • None represents “no value”; test it with is None.
  • Convert types explicitly with int(), float(), str(), bool().

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What does "PYTHON"[1:4] give?

Show answer

Answer: B. "YTH"

The slice takes indexes 1, 2 and 3 (Y, T, H); index 4 is excluded.

Question 2 · multiple choice

What is type(10 > 7)?

Show answer

Answer: C. bool

A comparison produces True or False, which are of type bool.

Question 3 · multiple choice

What does this program print?

Python · quiz_convert.py
a = "5"
b = 2
print(int(a) * b, a * b, float(b))
Show answer

Answer: D. 10 55 2.0

int(a) * b is 10; a string times an integer repeats it, giving “55”; float(2) is 2.0.

Question 4 · multiple choice

Which statement about strings is correct?

Show answer

Answer: C. s[-1] gives the last character

Negative indexes count from the end. Strings are immutable, there is no char type, and len() returns the number of characters (one more than the last index).

Question 5 · multiple choice

What does int(7.9) return?

Show answer

Answer: A. 7

int() truncates towards zero; use round(7.9) to get 8.

Question 6 · short answer

Write statements that take s = "Programming" and print its first three characters, its last three characters and the string reversed, and give the outputs.

Show worked answer

print(s[:3]) prints Pro; print(s[-3:]) prints ing; print(s[::-1]) prints gnimmargorP. The slice [:3] stops before index 3, [-3:] starts three characters from the end, and a step of −1 walks the string backwards.

↑ ↓ move↵ openTab switch type