Python Programming

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/20

encourage image

There's no tags or description

Looks like no tags are added yet.

21 Terms

1
New cards

print()

  • displays text or output values

  • automatically ends line with a new line


2
New cards

end=” ” parameter

  • allows you to print consecutive print statements on same line of code

  • if you want a space between the statements you must include space w/in the quotation marks


3
New cards

In what simple way could you update this code to display the following print statements on the same line?

print("I think I like")
print("apples")


Add the end parameter to first print statement

 print("I think I like", end=" ")
print("apples")


4
New cards

What is the difference between end=" " and and=""

space b/w quotation marks adds a space between separate print statements when the end parameter is used.


for example:

# NO SPACE
print("I like going to the mall", end="")
print("to window shop.")
#Output: I like going to the mallto window shop.

# WITH SPACE
print("I like going to the mall", end=" ")
print("to window shop.")
#Output: I like going to the mall to window shop.


5
New cards

How do you combine text and numbers in a single print statement?

by separating them w/in print statement with commas

#Example	
print("I am", 35, "years old.")


6
New cards

Name the 3 major uses for comments.

  1. To explain your code

  2. To make code more readable

  3. To prevent execution when testing code


7
New cards

T or F: A variable is created the moment you first assign a value to it.

True

8
New cards

T or F: In python, variable don’t need to be declared with any particular data type.

True

9
New cards

T or F: In Python, the data type of a variable can be changed after they’ve been set.

True.

10
New cards

What is “Type Casting”?

  • AKA “Casting”

  • Converting a variable or value from one data type to another


1) Explicit Type Casting → using Python’s built-in constructor functions (e.g. int() or bool() )

x = str(100)    # Converts integer 100 to string "100"
y = str(4.5)    # Converts float 4.5 to string "4.5"

2) Implicit Type Casting → Python’s interpreter converts data types automatically

#Each line of code below demonstrates implicit casting
x = 5     # Integer
y = 2.5   # Float

result = x + y  # Python automatically casts 'x' to 5.0
print(result)   # Output: 7.5 (Float)


11
New cards

Which type of language is Python:

(a) Dynamically Typed

(b) Statically Typed


Briefly explain what this means.

(a) Dynamically Typed →

  • The runtime environment (or interpreter) checks the data type of variables/values in the program at runtime (as the code runs, as opposed to before the code runs).

  • Explicit data types aren’t required (e.g. x =5; don’t need to state that 5 is an integer)

  • Data type of variable can change over time

  • Errors are only caught when line of code is executed


12
New cards

T or F: Variable names are case sensitive.

True

13
New cards

type()

returns data type of a variable/object

IMP NOTE: You must use the print() function with type() function in order for the data type to actually be displayed in output

minPassed = 50 
print(type(minPassed)) #Output: <class 'int'>

lettersLeft = ["a", "p", "x", "z"]
print(type(lettersLeft)) #Output: <class 'list'>


14
New cards

Rules for Python variables

  • A variable name must start with a letter or the underscore character

    • A variable name cannot start with a number

  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )

  • Variable names are case-sensitive (age, Age and AGE are three different variables)

  • A variable name cannot be any of the Python keywords.


15
New cards

Write a single line of code where:

  • each letter a through d is a variable

  • each variable holds string, which is the name of an animal starting with same letter


a, b, c, d = "ant", "bull", "cow", "dolphin"


16
New cards

How would you assign the same value of “Orange” to each of the variables x, y, and z — in one single line of code?

x = y = z = "Orange"
print(x)
print(y)
print(z)


17
New cards

Write the appropriate code:

Given the list fruits = ["apple", "banana", "cherry"], assign a fruit to each separate variable: (x, y, and z) using a single line of code.

Then print each variable on a separate line.

fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print(x)
print(y)
print(z)


18
New cards

What are the 2 ways to print multiple variables in a single print statement?

What’s the difference?

w/in print statement — separate each variable w/ a comma or a plus sign:

x = "Python"
y = "is"
z = "awesome"
print(x, y, z)
x = 5
y = "John"
print(x, y)

commas

  • automatically adds space at end of each variable

  • you can also print multiple variables of different data types, but with plus symbol you can only print variables of same data type togher

x = "Python "
y = "is "
z = "awesome"
print(x + y + z)
x = 5
y = "John"
print(x + y)
#RESULTS IN ERROR


19
New cards

What will happen when the following code is run?

x = 5
y = "John"
print(x + y)

How can this be fixed?

Python will give you an error because + symbol can’t be used to print variables of diff data types in same print statement.


Instead use commas to separate the variables.

20
New cards
21
New cards