
Data Types and Variables in Python
Learn about the fundamental data types in Python such as numbers, strings, and logical data, along with how variables are used to store information. Explore the concepts explained by Professor John Carelli from Kutztown University's Computer Science Department.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Data and Data Types Computer programs are written to store and manipulate information, or data Python supports 3 major types of information (i.e. data types) Numbers Strings one or more characters alphabetic, numbers, symbols, Logical: True or False Professor John Carelli Kutztown University Computer Science Department
Numerical Data Two broad categories Integers represent whole numbers No fractions or decimals positive and negative counting numbers, incl. zero 1, 25, 0, -100, etc Floating point numbers Fractional numbers (i.e. those with decimal points) Can be represented in either decimal or scientific notation Decimal notation: 3.14159 Scientific notation: 2.75e3 (mantissa and exponent) same as 2.73x103 Professor John Carelli Kutztown University Computer Science Department
Strings Python recognizes alphanumeric characters enclosed in quotes as a string (as opposed to a number) Like a name, or a sentence Can use either single, double (or even triple quotes!) Examples: A (a single character) John Hello World! Professor John Carelli Kutztown University Computer Science Department
Logical Data Represents conditional values Supports only two values: True or False Note: True and False are not strings no quotes! Sometimes referred to as Boolean data Named for George Boole English mathematician (mid 1800 s) developed Boolean Algebra Used in decision making statements More on this later . Professor John Carelli Kutztown University Computer Science Department
Variables Variables are used to store information A variable is a named memory location that contains the stored information Name is selected by the programmer see next slide for naming rules Professor John Carelli Kutztown University Computer Science Department
Valid variable names May only contain letters, numbers, and underscore (_) Good: name, x, abc123, last_name Bad: last-name Cannot begin with a number Bad: 1letter Are case-sensitive Name is not the same as name Cannot be Python Keywords Keywords are words reserved for use by a programming language Refer to table 2.1 (page 25) in your book Professor John Carelli Kutztown University Computer Science Department
Variable creation via assignment Variables are created (or updated) automatically whenever an assignment operation is performed Assignment uses the = operator Syntax: variable = value variable : a valid variable name value : the value to be stored Python automatically infers the data type for the variable based on the value being stored Number (int or float) vs. String vs. Boolean Professor John Carelli Kutztown University Computer Science Department
Assignment examples Using a fixed literal value x= 2 PI= 3.14159 name= Sam Using another variable x= y Using the result of an expression (more on this later ) twoPI= 2.0*PI Using a function return value (more on this later ) name = input( Enter your name: ) Professor John Carelli Kutztown University Computer Science Department
Literals Literal is a term used to describe an explicit, fixed data value. As opposed to a value that is the result of a computation (like z=x+y;) Any variable can be assigned a literal value Using the assignment (=) operator The variable then has that data type integer assignment i= 10 j= 0 k= -23 10, 0 and -23 are literals Professor John Carelli Kutztown University Computer Science Department
Floating point Literals Scientific Notation Two parts Mantissa the decimal number before the e Exponent the characteristic (exponent) is the integer after the e Floating point data x= 3.14159 // fixed notation y= 3.2e5 // scientific notation z= -15e-10 // neg value, neg exponent Numerical values above are floating point literals number= mantissa x 10characteristic Professor John Carelli Kutztown University Computer Science Department
String Literals Hello World! Strings can be enclosed in single, double, or even triple quotes Allows for strings with embedded quotes Triple quotes preserve new line characters Often used in comments and in strings intended to span multiple lines of output Pennsylvania A Sam s club this is a triple quoted string with embedded newline characters triple double quotes! Examples of String Literals Professor John Carelli Kutztown University Computer Science Department
Escape Characters Use a backslash character \ to modify the interpretation of the character immediately following in a string \ and \ treat the quotes as literal characters in a string \n is used to embed a new line character in a string For example: Sam\ s club Will treat the second as a literal apostrophe, not the end of the string msg= Hello\nworld Puts a new line between the words This would get printed on 2 lines, like this Hello world Professor John Carelli Kutztown University Computer Science Department
Constants Sometimes we want to define a variable whose value is fixed, i.e. cannot be changed Some languages, like C++, have a mechanism for enforcing this Python does not... however ... Example Constants PI= 3.1415926 SALES_TAX= 0.06 Convention for constants Use all uppercase letters Separate words with underbars Note: value can still be changed, but users understand not to Professor John Carelli Kutztown University Computer Science Department