🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

CT5 Basic Computations.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Transcript

Basic Computations COURSE TOPIC 5 INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Data Types Every problem involves some sort of data in it. The data can be: Numbers Integers (e.g. 24, 5874, -547) Floating (e.g. 45.214, 0.2547, -658.748)...

Basic Computations COURSE TOPIC 5 INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Data Types Every problem involves some sort of data in it. The data can be: Numbers Integers (e.g. 24, 5874, -547) Floating (e.g. 45.214, 0.2547, -658.748) String (e.g. “computer”, ‘ICT110’, “a”, ‘f’, ‘#’ ) Boolean (Logical) (True and False) The type of data is known as data type of that data item. Data Types Data Item Example Value Data Type Age of a person 15 Integer Current year 2022 Integer Radius of circle 27.58 Floating Value of PI 3.14159 Floating A vowel e String A key on keyboard ? String Person’s name Ali String Address of an office Department of CSE-MUET String Is 1st number greater than 2nd ? true Boolean Is 7 less than 5 ? false Boolean Data Literals A fixed value that any data type can take is called as literal. A number literal is always written without quotes. (15, 68, 25.14, 578.14) An string literal is written in single or double quotes. “computer”, ‘ICT110’, “a”, ‘f’, ‘#’ ) Multiline strings can be denoted using triple quotes (‘’’ OR “””). '''This is Multiline string''' A Boolean literal is always written without quotes. (True, False) Data Literals Data Literal Type of Literal ‘Ali’ String ‘b’ String 25.2 Floating “87.5” String “4” String 4 Integer 4.0 Floating “true” String false Boolean “Rashed” String Identifiers An identifier is a name given to a variable, function, class or module. – Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_). – A Python identifier can begin with an alphabet (A – Z and a – z and _). – An identifier cannot start with a digit but is allowed everywhere else. 1plus is invalid, but plus1 is perfectly fine. – Example: myCountry, other_1, good_morning, plus1 Keywords Keywords are a list of reserved words that have predefined meaning. Keywords are special vocabulary and cannot be used by programmers as identifiers for variables, functions, constants or with any identifier name. Statements A statement is an instruction that the Python interpreter can execute. Python program consists of a sequence of statements. Statements are everything that can make up a line (or several lines) of Python code. Example: z = 1 is an assignment statement. Expressions Expression is combination of variables, operators and values which are evaluated to make a new value. Expressions are statements as well. Examples: >>> 20 >>> z >>> z + 20 Variables In programming, a variable is the memory (RAM) location that can store the data temporary. Every variable has: Name Data type Value In Python, there is no need to declare a variable explicitly by specifying whether the variable is an integer or a float or any other type. Variables The name of the variable is called as the identifier. radius 25.5 In first example, we have a variable whose name is radius, its data types is floating 25.5 and its value is 25.5. option In second example, we have a variable whose name is option, its data type is string and its value is b. ‘b’ Legal Variable Names Variable names can consist of any number of letters, underscores and digits. Variable should not start with a number. Python Keywords are not allowed as variable names. Variable names are case-sensitive. For example, computer and Computer are different variables. Assigning Values to Variables In Python, variables are created when you assign a value to it. Syntax: variable_name = expression Example: x=5 y = “Hello World!” Assigning Values to Variables That value is stored in the variable on the execution of the assignment statement. Example: >>> number =100 In Python, not only the value of a >>> miles =1000.0 variable may change during program execution but also the type of data >>> name ="Python" that is assigned. >>> number 100 You can assign an integer value to a variable, use it as an integer for a while >>> miles and then assign a string to the variable. 1000.0 >>> name 'Python' Assigning Values to Variables A new assignment overrides any previous assignments. Example: >>> century = 100 >>> century 100 >>> century = "hundred" >>> century 'hundred' Assigning Values to Variables Python allows you to assign a single value to several variables simultaneously. Example: >>> a = b = c =1 >>> a 1 >>> b 1 >>> c 1 Operators Operators are symbols that perform certain mathematical or logical operation to manipulate data values and produce a result based on some rules. An operator manipulates the data values called operand. Python supports the following operators: a) Arithmetic Operators b) Assignment Operators c) Comparison Operators d) Logical Operators Arithmetic Operators Example Operator Operator Name Description (p = 2, q = 3) + Addition Produces the sum of two operands. p+q=5 - Subtraction Produces the difference of two operands. p - q = -1 * Multiplication Produces the product of the operands. p*q=6 / Division Produces the quotient of its operands q / p = 1.5 where the left operand is the dividend, and the right operand is the divisor. % Modulus Divides left operand by right operand and q%p=1 returns a remainder. ** Exponent Performs exponential (power) calculation p ** q = 8 on operators. // Floor division Returns the integral part of the quotient. 9 // 2 = 4 and 9.0 // 2.0 = 4.0 Assignment Operators Assignment operators are used for assigning the values generated after evaluating the right operand to the left operand. Operator Operator Name Example = Assignment z =p + q assigns value of p + q to z += Addition Assignment z += p is equivalent to z = z + p -= Subtraction Assignment z -= p is equivalent to z = z – p *= Multiplication Assignment z *= p is equivalent to z = z * p /= Division Assignment z /= p is equivalent to z = z / p **= Exponentiation Assignment z **= p is equivalent to z = z ** p //= Floor Division Assignment z //= p is equivalent to z = z // p %= Modulus Assignment z %= p is equivalent to z = z % p Comparison Operators The output of comparison operators is always a Boolean value, either True or False. Example Operator Operator Name (p =10, q = 20) == Equal to (p == q) is False != Not equal to (p != q) is True > Greater than (p > q) is False < Lesser than (p < q) is True >= Greater than or equal to (p >= q) is False =, 90 3) score < ((min/2) - 10) OR score > 90 4) (score < ((min/2) - 10)) OR score > 90 5) (score < ((min/2) - 10)) OR (score > 90) Type Conversions In Python, you can explicitly cast or convert a variable from one type to another. – The int() function to explicitly convert a float number or a string to an integer. – The float() function returns a floating-point number constructed from a number or string. – The str() function returns a string which is fairly human readable. Type Conversions - examples int() casting function str() casting function x = int(11) # x will be 11 x = str("s1") # x will be 's1' y = int(22.8) # y will be 22 y = str(22) # y will be ‘22' z = int("33") # z will be 33 z = str(33.0) # z will be '33.0' float() casting function x = float(11) # x will be 11.0 y = float(22.8) # y will be 22.8 z = float("33") # z will be 33.0 w = float(“44.2") # w will be 44.2 INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING The type() Function The type() function returns the data type of the given object. Syntax: type(object) Examples: >>> type(1) >>> type(6.4) >>> type("A") >>> type(True) Dynamic and Strongly Typed Language Python is a dynamic language as the type of the variable is determined during run-time by the interpreter. Python is also a strongly typed language as the interpreter keeps track of all the variables types. Dynamic and Strongly Typed Language In a strongly typed language, you are simply not allowed to do anything that’s incompatible with the type of data you are working with. Example: 1. >>> 5 + 10 15 2. >>> 1 + "a" Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for +: 'int' and 'str' Any Questions INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING

Tags

python programming data types introduction to programming
Use Quizgecko on...
Browser
Browser