m1_p2-types_and_expressions.pptx
Document Details
Uploaded by WellRoundedOlivine
Tags
Related
- Csc 102 Lecture 4: Introduction to Programming (Python) PDF
- Advanced Programming in Python PDF
- ITP 313 Python Fundamentals and Data Types PDF
- BSc (Hons) Electronics Ist Sem - Programming Fundamentals using Python PDF
- Meeting 2 - Fundamentals of Python Programming - s.en.ar.pptx PDF
- Python Data Types PDF
Full Transcript
CSE 1321 Module 1 – Part 2 Data types, Expressions, Operators Topics Data Types Python Data Types Concatenation Escape sequences Converting between types Comments Expressions Operators Types of operators Operator precedence Data Types Refers to the...
CSE 1321 Module 1 – Part 2 Data types, Expressions, Operators Topics Data Types Python Data Types Concatenation Escape sequences Converting between types Comments Expressions Operators Types of operators Operator precedence Data Types Refers to the type of data a variable is storing Knowing the data type lets us know what information a variable could be storing It also lets us know the type of the result of an operation could be In some programming languages, types also tell us how much space in memory is being used Different programming languages may have different built-in types Python Principal Built-in Types Numerics: All numbers Integers, floating-point numbers, complex numbers Boolean value (technically numeric) Sequences: ordered collections (more on this later) Lists, tuples, ranges Strings (technically sequences of characters) Mappings: key/value collections (more on this later) Dictionaries Classes Instances Exceptions Numeric types Integers: whole numbers of any size E.g.: 12 Floating-point numbers: decimal numbers E.g.: 12.1 Have a precision of about 14 decimal places Also include “nan” (not a number), “-inf” and “+inf” Complex: Numbers with a real and an imaginary part E.g.: 12+12j Boolean: True or False In Python, True and False must always have their first letter capitalized Strings A sequence of 0 or more characters Enclosed by “double quotes” in other languages Python accepts either ‘single’ or “double” quotes (cannot mix) They are immutable, meaning they cannot change in memory While it may seem that we are changing their contents when we assign it a new value, from the computer’s perspective, we are telling it to create a new string Strings have things they can do (later): len( ) – determines the number of characters replace() – replaces part of the string with another string lower() and upper() – changes the strings casing isnumeric() – checks if the string is a number split() – useful method for splitting a string Printing & String Concatenation (the + operator) String literals cannot span multiple lines (with some exceptions) + combines string literals with other data types for printing Example: print("Hello" + " " + "there"); ◦Output: Hello there Printing Escape Sequences Printing an escape sequence prints a special character in an output string. Common escape sequences: ◦\b is backspace. (e.g “B\bsecz\bret” prints what?) ◦\t is tab ◦\n newline ◦\r carriage return ◦\” double quote ◦\’ single quote ◦\\ backslash Converting between types To string: str() Pretty much anything can be converted to a string Be careful when converting anything else to a string! To integer: int() Floats will drop off the decimal part Anything else will likely crash! To floating-point: float() Integers will now have a decimal part Anything else will likely crash! To complex: complex() Comments You can leave comments to yourself or to other developers Comments are lines of code which are ignored by the computer Useful for leaving notes explaining portions of code Can be single line or multi-line # this is a single line comment ‘‘‘this is a multi -line comment’’’ Expressions An expression is a combination of one or more operators and operands It has both a data type and a value that results from the expression evaluation Operands may be: – literals (data written directly into the code) – constants (not present in Python) – variables Arithmetic Operators (Reminder) Math operators Addition (+) Note the + sign can also be used for concatenation Subtraction (-) Multiplication (*) Power (**) Division (/) Floor Division (//) Modulus (%) Division & Modulus When dividing two values: o If using a single /, the result is a decimal number (even if the division has no remainder) o If using //, the result could be a decimal or whole number, rounded down to the nearest whole number If one of the operands is a decimal number, the result is a decimal number To get the remainder, use the modulus operator with the same operands 24 / 3 = 8.0 25 / 3 = 8.3 25 // 3 = 8 25.0 // 3 = 8.0 25 % 3 = 1 25.0 % 3 = 1.0 Be mindful about data types in expressions! They determine what kind of operations you can do input() will always produce a string In other languages, adding a string to a number is always a concatenation In Python, strings can only be concatenated (+) with other strings, but not with numbers Strings can be multiplied by an integer (what does this do?) Any arithmetical operation between two integers will produce an integer (except division) Any arithmetical operation between an integer and a float will produce a float Comparison Operators Equality (==) NOT =, single = means ASSIGNMENT Inequality (!=) Greater than (>) Less than (=) Less than or Equal (