[go: up one dir, main page]

0% found this document useful (0 votes)
4 views31 pages

Week 3 MAK

The document provides an overview of basic data types in Python, including integers, floats, fractions, decimals, and complex numbers. It covers input/output operations, data conversion, built-in functions for numerical operations, and the use of the random package. Additionally, it explains multiple assignments and mixed-type operations in Python.

Uploaded by

nixak34028
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views31 pages

Week 3 MAK

The document provides an overview of basic data types in Python, including integers, floats, fractions, decimals, and complex numbers. It covers input/output operations, data conversion, built-in functions for numerical operations, and the use of the random package. Additionally, it explains multiple assignments and mixed-type operations in Python.

Uploaded by

nixak34028
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

WEEK 3: BASIC DATA TYPES IN

PYTHON

INS 107E Introduction to Programming Languages (Python)

05.03.2025
Dr. Merve AKBAŞ KAPLAN 1
Basic Data Types in Python

2
Basic Data Types in Python

3
Basic Input/Output (Keyboard/Screen)

>>> x = input('Write something: ')


Write something: test
>>> print('You wrote: ' + x)
You wrote test

4
Basic Input/Output (Keyboard/Screen)

5
Importing Packages in Python

6
Types of Numerical Objects in Python
1. Integers
2. Floats
3. Fractions
4. Decimals
5. Complex numbers

7
1. Integers

8
2. Floats

9
2. Floats

10
2. Floats

11
2. Floats

12
3. Fractions

13
4. Decimals

14
5. Complex Numbers

2
𝑐 = 𝑅2 + 𝐼𝑚

15
Operations in Python

16
Operations in Python

17
Order of Precedence

18
Multiple Assignments

Operation Description
x += y x = x + y
x -= y x = x - y
x *= y x = x * y >>> x=15;y=24
x /= y x = x / y or
x **= y x = x ** y >>> x,y=15,24
x %= y x = x % y >>> x += y
>>> x
39

19
Multiple Assignments

>>> x=15;y=24
or
>>> x,y=15,24
>>> x += y
>>> x
39

20
Mixed-type Operations

21
Data Conversion
Operation Meaning of the Operation Input Command Output
float(x) Converts a string or number to a float. float(16) 16.0

int(x) Converts a string or number to an integer int(16.44) 16

complex(x,y) Create a complex number with real part ‘x’ complex(2,-6) (2-6j)
and imaginary part ‘y’.
bin(integer) Return the binary repr. of an integer. bin(16) '0b10000'

hex(integer) Return the hexadecimal repr. of an integer. hex(16) '0x10'

oct(integer) Return the octal repr. of an integer. oct(16) '0o20'

str(object) Converts a variable to a string. "Asli's age is " "Asli's age


+ str(36) is 36"
bool(x) Converts a variable to boolean. bool(16) True
22
Data Conversion

23
Reading Data From The Keyboard

24
Built-in Functions for Numbers

Operation Description Input Command Output

abs(x) absolute value or magnitude of x abs(-229.76) 229.76

round(number[, ndigits]) rounds a number to a given round(396.864,1) 396.9


precision
pow(x, y[, z]) returns x to the power of y pow(10,2,3) 1
(modulo z) pow(10,4%3) 10
divmod(x, y) the pair (x // y, x % y) divmod(32,3) (10, 2)

25
Built-in Functions for Numbers

26
eval FUNCTION
▪ eval() evaluates the passed string as a Python expression and returns the result.

>>> function=input('Type a function: f= ')


Type a function: f= x**2+4*x+3
>>> x=3
>>> eval(function)
24 27
math Module

>>> import math


>>> math.sin(math.pi)
1.2246467991473532e-16
>>> math.sin(math.pi/2)
1.0
28
random Package

Operation Description Input Command Output

random.random() Generates a random float (0<x<1) random.random() 0.9326930


313708954
random.seed([object]) The random module uses the seed random.seed("ali") 0.7965825
value as a base to generate a random.random() 690601995
random number.
random.randint(a, b) Return a random integer in range [a, random.randint(1, 44
b], including both end points. 100)
random.choice([ ]) Perform a random selection from a random.choice(['Ali', 'Mehmet'
list 'Ahmet','Ibrahim'
,'Mehmet','Hasan'])

29
random Package

30
random Package

31

You might also like