Full Transcript

IT 403 Elective 5 (Web Systems and Technologies 3) Lesson 2: Variables, Data Types, Input and Output Instructor: Genalyn D. Villafuerte Variables Variables are containers for storing data values. A variable is created the moment you first assign a value to it. name = "John D...

IT 403 Elective 5 (Web Systems and Technologies 3) Lesson 2: Variables, Data Types, Input and Output Instructor: Genalyn D. Villafuerte Variables Variables are containers for storing data values. A variable is created the moment you first assign a value to it. name = "John Doe" age = 40 Variables Variables do not need to be declared with any particular type, and can even change type after they have been set. x = 4 # x is of type int x = "Python" # x is now of type str print (x) Variables Data Types Type of data stored in a variable String x = "Hello World" x = ‘Hello World’ Integer x = 214 Float x = 2.14 Boolean x = true Bytes bytes(4) Data Types Naming Variables Naming Variables Legal variable names Illegal variable names input() function Accepts user’s input name = input("Enter Full Name: ") email = input("Enter Email: ") print() function Displays the program’s output print("Name: " + name) print("Email: " + email) input() and print() sn = input("Enter Student Number: ") fn = input("Enter First Name: ") ln = input("Enter Last Name: ") sc = input("Enter Course: ") print("Your Name is " + ln + ", " + fn) print("Student Number is " + sn) print("Course is " + sc) print() function The empty print() invocation is not as empty as you may have expected – it does output an empty line, or (this interpretation is also correct) its output is just a newline. The backslash (\) has a very special meaning when used inside strings – this is called the escape character. print() function The arguments are separated by commas. Can display the result of the arithmetic expression Naming Variables Assigning values to variables Outputting values of variables Outputting values of variables Casting Converting one data type into another is known as casting. Casting in Python is done using constructor functions: str() – constructs a string from a wide variety of data types, including strings, integer literals, and float literals int() – constructs an integer number from an integer literal, a float literal (by removing all decimals), or a string literal (providing the string represents a whole number) float() – constructs a float number from an integer literal, a float literal, or a string literal (providing the string represents a float or an integer) Casting Converting one data type into another x = int(10) x = "10" print("Number is " + str(x)) y = "10" print(int(x) + int(y)) Output if str is removed: Output if int is removed : Type Casting and Inputting num1 = input("Enter 1st number:") num2 = input("Enter 2nd number:") sum = num1 + num2 print("The sum of the two numbers is ", sum) num1 = input("Enter 1st number:") num2 = input("Enter 2nd number:") sum = int(num1) + int(num2) print("The sum of the two numbers is ", int(sum)) num1 = float(input("Enter 1st number:")) num2 = float(input("Enter 2nd number:")) sum = num1 + num2 print("The sum of the two numbers is ", float(sum)) Arithmetic Operators  Python codes Sample output → Comparison Operators Python codes Strings String - series or characters interpreted as text. “The quick brown fox.” ‘The fast green turtle’ “This milk costs 30 pesos” “Mike said ‘I love eating burger!’” ‘John said “I don\’t like vegetables”’ “This next text will move to the \nnext line” Formatted Printing: Strings Placeholders Container for strings and numbers {} % f Strings Placeholders {} name = "Gen" food = "pizza" game = "volleyball" text1 = "My name is {} i love {} and playing {}" text = text1.format(name, food, game) print(text) Strings Placeholders {} name = “Gen” food = “pizza” game = “volleyball” text2 = "My name is {2} i love {1} and playing {0}" text = text2.format(name, food, game) print(text) Strings Placeholders {} name = “Gen” food = “pizza” game = “volleyball” text3 = "My name is {newname} i love {newfood} and playing {newgame}" text = text3.format(newname=“Ana", newfood=“noodles", newgame=“tennis") print(text) Strings Placeholders % items = "milks" cost = 335.50 qty = 55 total = cost * float(qty) text4 = "The %d %s cost %.2f each. \nThe total cost is %.2f." % (qty, items, cost, total) print(text4) f-String F-String was introduced in Python 3.6, and is now the preferred way of formatting strings. To specify a string as an f-string, put an f in front of the string literal items = "milks" cost = 335.50 text5 = f"The product costs 100 pesos." print(text5) text6 = f"The product {items} cost {cost * 100} pesos." print(text6) f-String A placeholder can also include a modifier to format the value. A modifier is included by adding a colon (:) followed by a legal formatting type, Example:.2f which means fixed point number with 2 decimals items = "milks" cost = 335.50 text7 = f"The product {items:.2f} cost {cost:.2f} pesos." print(text7) text7 = f"The product {items:.2f} cost {100:.2f} pesos." print(text7) f-String You can perform math operations on variables. You can also use a comma as a thousand separator. price = 978 tax = 0.12 txt = f"The price is {price + (price * tax):,.3f} pesos" print(txt) Number Formatting Functions Number data types store numeric values. They are immutable data types, which means that changing the value of a number data type results in a newly allocated object. round() – returns a floating point number that is a rounded version of the specified number, with the specified number of decimals ceil() – rounds a number UP to the nearest integer, if necessary, and returns the result floor() – rounds a number DOWN to the nearest integer pow() – returns the value of x to the power of y (xy) Number Formatting Functions import math g0 = 77.00001 g1 = 95.537 g2 = 95.20 print(round(g1, 2)) print(math.ceil(g0)) print(math.floor(g1)) print(pow(5, 2)) print(5**2)

Use Quizgecko on...
Browser
Browser