Programming Essentials in Python - Ch 3 PDF
Document Details
Uploaded by IntuitiveExtraterrestrial8596
Tags
Related
Summary
This document is a chapter on basic data types and operators in Python programming. It explains different data types like numbers, strings, lists, tuples, and dictionaries, along with operators and their uses.
Full Transcript
Chapter 3: Basic data types and operators Python, a widely used high-level programming language, offers various built-in data types and operators. Here's a brief overview of both. 1.1 Operators These are special symbols which help the user to carry out operations like addition, subtraction, etc. Py...
Chapter 3: Basic data types and operators Python, a widely used high-level programming language, offers various built-in data types and operators. Here's a brief overview of both. 1.1 Operators These are special symbols which help the user to carry out operations like addition, subtraction, etc. Python provides following type of operators: Arithmetic operators: +, –, *, /, %, ** and //. Assignment operators: =, + =, – =, *=, /=, %=, **= and //= Logical operators: or, and, and not Relational operators: =, != or < > and ==. 1.2 Standard Data Types Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, data types are actually 1 classes and variables are instances (object) of these classes. The data stored in memory can be of many types. Python has five main standard data types: Numbers String List Tuple Dictionary Numbers Number data types store numeric values. Number objects are created when you assign a value to them. Python supports different numerical types: int for integer numbers : Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length. float for decimal numbers: Float, or "floating point number" is a number, positive or negative, containing one or more decimals. complex for complex numbers: Complex numbers are written with a "j" as the imaginary part: Variables of numeric types are created when you assign a value to them. For example x = 1 # int y = 2.8 # float z = 1j # complex 2 for example: 3 String A string is a collection of one or more characters put in a single quote, double- quote, or triple-quote. In python there is no character data type, a character is a string of length one. It is represented by str class. Creating String: Strings in Python can be created using single quotes or double quotes or even triple quotes. As you’ve already seen, you can create a string by surrounding some text with quotation marks: string1 = 'Hello, World' string2 = "1234" You can use either single quotes (string1) or double quotes (string2) to create a string as long as you use the same type at the beginning and end of the string. 4 Whenever you create a string by surrounding text with quotation marks, the string is called a string literal. The name indicates that the string is literally written out in your code. All the strings you’ve seen thus far are string literals. A string is organized as an array of characters. For example: Strings (Access character): Example 1: Example 2: 5 Example 3: To access a substring from a string, we use a string variable name followed by square brackets with starting index and ending index of the required substring. 6 For example: Strings (+, * operators) 7 Strings (Functions) Example 1: Example 2: 8 List Python Lists are just like dynamically sized arrays. A list is a collection of things, enclosed in [ ] and separated by commas. The list is a sequence data type which is used to store the collection of data. Tuples and String are other types of sequence data types. For example: Here we are creating Python List using []. Var = ["Geeks", "for", "Geeks"] print(Var) Output: ["Geeks", "for", "Geeks"] 9 Tuple A Tuple is a sequence, just like a list. The differences between tuples and lists are: the tuples cannot be changed unlike lists tuples use parentheses (), whereas lists use square brackets []. Parentheses are optional in tuples. Creating Python Tuples: How to access elements from tuple? 10 How to remove the entire tuple? Tuples (+, * operators) 11 Lists (Functions) Exercises: Lists & Tuples 12 Dictionary Dictionary in Python is a collection of keys values, used to store data values like a map, which, unlike other data types which hold only a single value as an element. For example: Dict = {1: 'Geeks', 2: 'For', 3: 'Geeks'} print(Dict) 13