knowt logo

ITSS 4381 - 08.30

Page 1

  • Data Types

  • Vatsal Maru - Assistant Professor of Instruction

  • JSOM 4.414

Page 2: Constants

  • Fixed values - numbers, letters, and strings

    • print (123) - prints the number 123

    • print (98.6) - prints the number 98.6

    • print ('Hello world') - prints the string 'Hello world'

Transcript Notes

Page 3: Reserve Words

  • Reserve words are words that cannot be used as identifiers

  • Examples of reserve words:

    • del

    • global

    • not

    • with

    • as

    • elif

    • if

    • or

    • yield

    • assert

    • else

    • import

    • pass

    • break

    • except

    • in

    • raise

    • class

    • finally

    • is

    • return

    • continue

    • for

    • lambda

    • try

    • def

    • from

    • nonlocal

    • while

Page 4: Variables

Main Ideas:

  • Variables are used to store data in memory

  • Data can be accessed using the variable name

  • Variables can be changed later

  • Variable names should not be repeated

Supporting Details:

  • Variable Examples:

    • x = 5

    • temperature = 70

    • pi = 3.14

    • X = 7

Page 5: Variables in Python

Main Ideas:

  • Variable names (identifiers) must start with a letter (or_)

  • Variable names are case sensitive

  • If the variable names are long, copy and paste is a good practice

Supporting Details:

  • Variable names must start with a letter or an underscore (_) character

  • Variable names are case sensitive, meaning "x" and "X" are different variables

  • Using meaningful variable names can improve code readability

  • Copying and pasting long variable names can help avoid typing errors

  • Examples of valid variable names: x, temperature, pi

Page 6: Variables

  • Variable names are user defined, not predefined

  • Best practice is to name variables such that they are self-explanatory

  • This improves readability of code and debugging (troubleshooting)

    • hininches = 20

    • h_in_in = 20

    • height_inches = 20

Page 7

  • Python Basics Structures

    • y = x*7

    • print(y)

    • Note: 42

  • Assignment with expression

    • Type: str

    • Output: 'Hello, World!'

  • Variables

    • Variables: X, y, pi

  • Error: NameError

    • Operator: *

    • Line: 1

    • Error: name 'pi' is not defined

  • Constant

    • Constant: 7, 3.14

  • Assignment statement

    • Print statement: 3.14

  • Function

    • Functions: print, type

  • Usage of variables in functions or programs

    • Without creating a variable, we cannot use them in a function or program

Page 8: Variables

  • Variables are used in equations

  • Variables are updated based on computations

X = 10

y = 25

X = y * X * 0.5

X = 125

Page 9: Operator Precedence

  • Highest to lowest precedence in python

    • Parentheses

    • Exponent (raise to a power)

    • Division

    • Multiplication

    • Remainder

    • Addition

    • Subtraction

  • Operations are evaluated from left to right

Page 10: Operator Precedence

  • Question: Which operator takes precedence in Python?

  • Expression: + 4 * 6 - 8 / 10 ** 2

  • Output: 25.92

Page 11: Operator Precedence

  • For readability and debugging, use parentheses

  • Write the easiest way possible

  • Why?

    • Operations might not give errors

    • However, they might not return the intended results if they are computed wrongly in Python

Page 12: Identifier Types

  • In programming, we can differentiate variables, constants, or literals

  • In python, we have integers, floats, and strings

  • To know the type, we can use type() function

  • The operators also identify these different types

  • For example, + and * operators add and multiply numbers

  • + operator can also concatenate strings

  • * operator can multiply sequence

Page 13: Identifier Types

  • Operators:

    • + operator adds numbers

    • * operator multiplies numbers

  • Example:

    • Addition:

      • z = 1 + 2

      • print(z) Output: 3

    • Multiplication:

      • z = 1 * 2

      • print(z) Output: 2

    • String Concatenation:

      • z = "hello " + "world"

      • print(z) Output: hello world

Page 14

  • Identifier Types

    • Z = "hello " * "world"

      • Output: TypeError - can't multiply sequence by non-int of type 'str'

    • Z = "this " * 2

      • Output: this this

    • Z = "this" + 1

      • Output: TypeError - can only concatenate str (not "int") to str

Page 15: Type Conversions

  • Performing a math operation between integer and float, the resulting variable is automatically converted to a float

  • For number constants, the built-in functions such as int() and float() can convert them to integer or float, respectively

  • Integer divisions are float in Python 3 (earlier versions had it differently)

  • round() converts a float to the nearest integer

  • round() takes the optional second parameter to specify desired numbers after decimal point

Page 16: String Conversion

  • int() and float() can be used to convert strings

    • The int() function converts a string to an integer

    • The float() function converts a string to a floating-point number

  • Conversion only happens if the given string is a number

    • If the string is not a number, an error will occur

Page 17

  • User input in Python

    • Using the input() function

    • Example: age = input("Enter your age: ")

  • Output

    • Example: Enter your age: 50

    • Type of age variable is a string

Page 18: User Input Conversion

  • User inputs are strings

  • To convert, we can use int() or eval() or float() commands

  • eval() determines int or float type automatically

Example:

  • User input: age_ = input("Enter your age: ")

  • Output: Enter your age: 50

  • Conversion: age = eval(age_)

  • Type of age: int

Transcript Notes

Page 19

  • Comments

    • To comment, use # before writing

    • Anything after # will be text and not code

  • Use:

    • To experiment with the line(s) of code

    • To document what a line of code is doing

    • To describe to other members

Page 20

  • Thank you vatsal.maru@utdallas.edu JSOM 4.414

Page 1

  • Data Types

  • Vatsal Maru - Assistant Professor of Instruction

  • JSOM 4.414

Page 2: Constants

  • Fixed values - numbers, letters, and strings

    • print (123) - prints the number 123

    • print (98.6) - prints the number 98.6

    • print ('Hello world') - prints the string 'Hello world'

Transcript Notes

Page 3: Reserve Words

  • Reserve words are words that cannot be used as identifiers

  • Examples of reserve words:

    • del

    • global

    • not

    • with

    • as

    • elif

    • if

    • or

    • yield

    • assert

    • else

    • import

    • pass

    • break

    • except

    • in

    • raise

    • class

    • finally

    • is

    • return

    • continue

    • for

    • lambda

    • try

    • def

    • from

    • nonlocal

    • while

Page 4: Variables

Main Ideas:

  • Variables are used to store data in memory

  • Data can be accessed using the variable name

  • Variables can be changed later

  • Variable names should not be repeated

Supporting Details:

  • Variable Examples:

    • x = 5

    • temperature = 70

    • pi = 3.14

    • X = 7

Page 5: Variables in Python

Main Ideas:

  • Variable names (identifiers) must start with a letter (or_)

  • Variable names are case sensitive

  • If the variable names are long, copy and paste is a good practice

Supporting Details:

  • Variable names must start with a letter or an underscore (_) character

  • Variable names are case sensitive, meaning "x" and "X" are different variables

  • Using meaningful variable names can improve code readability

  • Copying and pasting long variable names can help avoid typing errors

  • Examples of valid variable names: x, temperature, pi

Page 6: Variables

  • Variable names are user defined, not predefined

  • Best practice is to name variables such that they are self-explanatory

  • This improves readability of code and debugging (troubleshooting)

    • hininches = 20

    • h_in_in = 20

    • height_inches = 20

Page 7

  • Python Basics Structures

    • y = x*7

    • print(y)

    • Note: 42

  • Assignment with expression

    • Type: str

    • Output: 'Hello, World!'

  • Variables

    • Variables: X, y, pi

  • Error: NameError

    • Operator: *

    • Line: 1

    • Error: name 'pi' is not defined

  • Constant

    • Constant: 7, 3.14

  • Assignment statement

    • Print statement: 3.14

  • Function

    • Functions: print, type

  • Usage of variables in functions or programs

    • Without creating a variable, we cannot use them in a function or program

Page 8: Variables

  • Variables are used in equations

  • Variables are updated based on computations

X = 10

y = 25

X = y * X * 0.5

X = 125

Page 9: Operator Precedence

  • Highest to lowest precedence in python

    • Parentheses

    • Exponent (raise to a power)

    • Division

    • Multiplication

    • Remainder

    • Addition

    • Subtraction

  • Operations are evaluated from left to right

Page 10: Operator Precedence

  • Question: Which operator takes precedence in Python?

  • Expression: + 4 * 6 - 8 / 10 ** 2

  • Output: 25.92

Page 11: Operator Precedence

  • For readability and debugging, use parentheses

  • Write the easiest way possible

  • Why?

    • Operations might not give errors

    • However, they might not return the intended results if they are computed wrongly in Python

Page 12: Identifier Types

  • In programming, we can differentiate variables, constants, or literals

  • In python, we have integers, floats, and strings

  • To know the type, we can use type() function

  • The operators also identify these different types

  • For example, + and * operators add and multiply numbers

  • + operator can also concatenate strings

  • * operator can multiply sequence

Page 13: Identifier Types

  • Operators:

    • + operator adds numbers

    • * operator multiplies numbers

  • Example:

    • Addition:

      • z = 1 + 2

      • print(z) Output: 3

    • Multiplication:

      • z = 1 * 2

      • print(z) Output: 2

    • String Concatenation:

      • z = "hello " + "world"

      • print(z) Output: hello world

Page 14

  • Identifier Types

    • Z = "hello " * "world"

      • Output: TypeError - can't multiply sequence by non-int of type 'str'

    • Z = "this " * 2

      • Output: this this

    • Z = "this" + 1

      • Output: TypeError - can only concatenate str (not "int") to str

Page 15: Type Conversions

  • Performing a math operation between integer and float, the resulting variable is automatically converted to a float

  • For number constants, the built-in functions such as int() and float() can convert them to integer or float, respectively

  • Integer divisions are float in Python 3 (earlier versions had it differently)

  • round() converts a float to the nearest integer

  • round() takes the optional second parameter to specify desired numbers after decimal point

Page 16: String Conversion

  • int() and float() can be used to convert strings

    • The int() function converts a string to an integer

    • The float() function converts a string to a floating-point number

  • Conversion only happens if the given string is a number

    • If the string is not a number, an error will occur

Page 17

  • User input in Python

    • Using the input() function

    • Example: age = input("Enter your age: ")

  • Output

    • Example: Enter your age: 50

    • Type of age variable is a string

Page 18: User Input Conversion

  • User inputs are strings

  • To convert, we can use int() or eval() or float() commands

  • eval() determines int or float type automatically

Example:

  • User input: age_ = input("Enter your age: ")

  • Output: Enter your age: 50

  • Conversion: age = eval(age_)

  • Type of age: int

Transcript Notes

Page 19

  • Comments

    • To comment, use # before writing

    • Anything after # will be text and not code

  • Use:

    • To experiment with the line(s) of code

    • To document what a line of code is doing

    • To describe to other members

Page 20

  • Thank you vatsal.maru@utdallas.edu JSOM 4.414