Search Header Logo
01 Python Basics

01 Python Basics

Assessment

Presentation

Computers

University

Practice Problem

Medium

Created by

Karen Ahmed

Used 138+ times

FREE Resource

14 Slides • 11 Questions

1

Python Programming

Lesson 1: Python Basics

media

2

Using Python​

Python runs in an Integrated Development Environment (IDLE)

Python provides a Python Shell, which can be used to execute single Python commands and display the result.

media

3

​Creating Programs

We need to create a new file to write our Python programs.

This will be a blank text file which is saved as a .py document (a Python file type)

media
media

4

​Print & Comments

  • The print() function will display a message to the shell

  • The message to be printed needs to be in brackets surrounded by quotes

  • Double or single quotes can be used, but it is important to remain consistent!

  • Comments in Python begin with the # character.

media

Before we can run Python code, we must first save the file.

Run the code by selecting Run>Run Module from the menu.

media
media

5

Multiple Choice

Question image

Which function outputs text to the terminal?

1

write()

2

print()

3

output()

4

type()

6

​Variables

  • We need to create variables to store values

  • In Python, we assign variables by using the equals sign =

  • Variables cannot have any spaces or symbols in their names other than an underscore.

  • Variable names cannot being with numbers, but they can have numbers after the first letter.

  • In Python, we do not have to specify a data type for our variables when we declare them.

  • The basic data type are int, float and string

media
media

7

Multiple Choice

Question image

Which of the following will produce a Syntax Error?

1

answer = "This is an error"

2

answer = this_is_an_error

3

answer = "This is an error'

4

All of them

8

Multiple Choice

Question image

What is the Python syntax for creating a variable and assigning the number 10 to it?

1

def variable_name = 10

2

variable_name == 10

3

variable_name = 10

4

int variable_name = 10

9

Multiple Choice

Question image

Which character is used to specify a comment in Python?

1

"

2

*

3

/

4

#

10

​Arithmetic Operations

  • When we perform division, the result has a decimal place.

  • This is because Python converts all ints to floats before performing division.

  • In older versions of Python (2.7 and earlier) this conversion did not happen and integer division would always round down to the nearest integer.

  • Performing arithmetic does not change the value of a variable, you can only update values using the = sign.

media

11

Multiple Choice

Question image

What will get printed if we run this code?

1

6

2

10

12

Multiple Choice

Question image

Now what will get printed?

1

6

2

10

13

​String Concatenation

  • The + operator does not just add two numbers

  • It can also “add” two strings

  • The process of combining two strings is called string concatenation

  • Performing string concatenation creates a brand new string comprised of the first string’s contents followed by the second string’s contents

media
media

​Concatenation does not include spaces between the strings!

14

​str()

  • To concatenate a string with a number, we need to convert the number into a string first.

  • We do this using the str() function.

  • We do not need to convert a number to a string for it to be an argument in a print statement.

media

15

Multiple Choice

Question image

How do you combine two strings?

1

string1.concatenate(string2)

2

string1.string2

3

string1.combine(string2)

4

string1 + string2

16

​Plus Equals

  • Python offers a shorthand for updating variables.

  • When you have a number saved in a variable and want to add to the current value of the variable, you can use the += (plus-equals) operator.

  • We can also use the plus-equals operator for string concatenation.

media
media

17

Multiple Choice

Question image

What is the value of total_cost that gets printed?

1

15

2

10

3

5

18

​User Input

  • We can output a value using print()

  • We can assign data to a variable using input()

  • The input() function requires a prompt message, which it will print out for the user before they enter their data.

  • ​For example:

  • ​Python always accepts user input as a string!

media
media

19

Multiple Choice

Question image

What is printed when we run this code if the user enters the values 5 and 6?

1

11

2

56

20

​Strings to Ints

  • ​If we want to work with numbers, for example integers, we need to convert the users input to an integer.

  • ​We can do this using the int() function.

  • ​Now the program will work as expected.

media

21

Multiple Choice

Question image

What is the output of this code?

1

210

2

42

3

cool_number

22

Multiple Choice

Question image

What happens if you run this code?

1

"message" is printed

2

"What a cool message!" is printed

3

Python throws a SyntaxError

23

​Exercise 1

  • ​ASCII art is a graphic design technique that consists of pictures made up of individual characters.

  • ​Write a Python program called initials.py that displays the initials of your name in block letters.

media
media

24

web page not embeddable

ascii-art - Replit

You can open this webpage in a new tab.

25

web page not embeddable

LovelyLoveseats - Replit

You can open this webpage in a new tab.

Python Programming

Lesson 1: Python Basics

media

Show answer

Auto Play

Slide 1 / 25

SLIDE