PYTHON VARIABLES AND TYPES

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

To define an integer, use the following syntax:

myint = 7 print(myint) This will output 7

To define a floating point number, you may use one of the following notations:

-myfloat = 7.0 -print(myfloat) OR -myfloat = float(7) -print(myfloat) Both output will be 7.0

what is the output of the following script: one = 1 two = 2 three = one + two print(three) hello = "hello" world = "world" helloworld = hello + " " + world print(helloworld)

3 hello world

How are Strings are defined either with a single quote or a double quotes.

Either with a single quote or a double quotes. e.g -mystring = 'hello' -print(mystring) -mystring = "hello" -print(mystring)

Is mixing operators between numbers and strings is not supported: eg # This will not work! one = 1 two = 2 hello = "hello" print(one + two + hello)

No; # This will not work! one = 1 two = 2 hello = "hello" print(one + two + hello) -This will output ERROR: Traceback (most recent call last): File "<stdin>", line 6, in <module> print(one + two + hello) TypeError: unsupported operand type(s) for +: 'int' and 'str'

What type of numbers does Python support?

Python supports two types of numbers - integers and floating point numbers. (It also supports complex numbers)

What is the difference between using double quotes and single quotes?

The difference between the two is that using double quotes makes it easy to include apostrophes (whereas these would terminate the string if using single quotes) eg. -mystring = "Don't worry about apostrophes" -print(mystring) Output: -Don't worry about apostrophes -Don't worry about apostrophes

Can assignments be done on more than one variable "simultaneously" on the same line like this: a, b = 3, 4 print(a,b)

Yes and the output is: 3 4

Is every variable in Python is an object?

Yes,every variable in Python is an object.

The target of this exercise is to create a string, an integer, and a floating point number. The string should be named mystring and should contain the word "hello". The floating point number should be named myfloat and should contain the number 10.0, and the integer should be named myint and should contain the number 20. Script: # change this code mystring = None myfloat = None myint = None # testing code if mystring == "hello": print("String: %s" % mystring) if isinstance(myfloat, float) and myfloat == 10.0: print("Float: %f" % myfloat) if isinstance(myint, int) and myint == 20: print("Integer: %d" % myint) -and what would be the output ?

mystring = "hello" myfloat = 10.0 myint = 20 # testing code if mystring == "hello": print("String: %s" % mystring) if isinstance(myfloat, float) and myfloat == 10.0: print("Float: %f" % myfloat) if isinstance(myint, int) and myint == 20: print("Integer: %d" % myint) OUTPUT: String: hello Float: 10.000000 Integer: 20


Kaugnay na mga set ng pag-aaral

Expressing permission with 可以

View Set

CompTIA A+ 220-1101 Domain 1: Mobile Devices

View Set

FHCE 4270E Final Exam Study Guide

View Set

FAR2:1 Notes to Financial Statements

View Set

Chapter 2: Software Process and Process Models

View Set