INFS1101 Python Basic Syntax

0.0(0)
studied byStudied by 2 people
full-widthCall with Kai
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/42

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

43 Terms

1
New cards

Python code general guidelines

  • Write one instruction per line

  • Indentation

2
New cards

Comment

  • Serve the purpose of offering explanations to the source code reader regarding the actions taking place within a specific code block where the comment is placed

  • When the code is executed, the interpreter disregards comments.

  • A single-line comment is initiated with the pound sign (#).

3
New cards

Indentation

It is of utmost importance as it indicates blocks of coded 

4
New cards

Print Function

print (“Hello“, “World“)

5
New cards

Variables

they are defined and assigned whenever needed

  • The assignment is using the = sign

6
New cards

letter or underscore

Every variable should start by a ____________, that can be followed by any series of letters, digits, or underscores.

7
New cards

reserved words

You cannot use _______ as variable names / identifiers.

- ex. if=40

8
New cards

True 

Variables in python are case-sensitive to letter case. 'myVariable' and 'myvariable' are seen as different variables.

9
New cards

int

integer numbers

10
New cards

the syntax /

•the result is the quotient which is a float

11
New cards

the syntax //

•the result is the quotient which is an integer

12
New cards

Float

floating-point numbers (decimal)

13
New cards

3.0

What does float(3) returns ?

14
New cards

Error

What does int("3.2") return ?

15
New cards

-2

What does int(-2.7) return ?

16
New cards

String

By default, in Python, the input function reads the user's input as a ______.

17
New cards

name=input("What is your name?")

output is saved in a variable, function name, one argument

18
New cards

int

We can convert the string output of the input function into an integer using the built-in ______ function

19
New cards

float

We can convert the string output of the input function into a float using the built-in ______ function

20
New cards

False

In Python, the code inside an f-string is not executed before it's printed.

21
New cards

Variable type - String

a list of characters, where each character is a letter, a digit or other symbols

22
New cards

String length

The number of characters in a string

23
New cards

greeting = HELLO

greeting[-1]

O

24
New cards

H

greeting = HELLO

greeting[0]

25
New cards

greeting[0:5]

greeting = "HELLO WORLD"

displays: 'HELLO'

26
New cards

displays: 'HELLO W'

greeting = "HELLO WORLD"

greeting[:7]

27
New cards

.upper()

converts a string to uppercase

28
New cards

.lower()

converts a string to lowercase

29
New cards

.capitalize()

converts the first letter of the string to uppercase

30
New cards

Decision in Python – Simple if Statement

if (condition):

instruction1_inside_if

instruction2_inside_if

instruction_outside_if

  • Colon (:) → required at the end of the condition

  • No then keyword → it is implicit

  • Indentation → determines what belongs inside the if block

  • Inside the if → instructions run only if the condition is true

  • Outside the if → runs regardless of condition outcome

  • Brackets around condition: optional if simple, good practice if complex

31
New cards

By indentation

How does Python know which instructions belong inside the if block?

32
New cards

Only if the condition is true

When are the instructions inside the if block executed?

33
New cards

No, else has no condition.

If-Else Statement

Is there a condition after else in Python?

34
New cards

The if block executes, then the instruction outside the if. The else block is skipped.

If-Else Statement
What happens if the condition is true?

35
New cards

The if block is skipped, the else block executes, then the instruction outside the if.

If-Else Statement

What happens if the condition is false?

36
New cards

When condition1 is false and condition2 is true.

if-elif-else statement
When is the elif block executed?

37
New cards

When all previous conditions are false.

if-elif-else statement

When is the else block executed?

38
New cards

Exactly one block.

if-elif-else statement
In an if-elif-else chain, how many blocks are executed?

39
New cards

Boolean operators

helps you make decisions with True and False. 'and' needs both to be True, 'or' needs at least one, and 'not' flips them around.

40
New cards

and Operator

  • Meaning: Both conditions must be True → result True

  • False if either condition is False

  • Example:

x = 5
y = 10
if x > 0 and y > 5:
    print("Both conditions are True")
else:
    print("At least one condition is False")

and → all must be True

41
New cards

or Operator

  • Meaning: At least one condition must be True → result True

  • False only if both conditions are False

  • Example:

x = 5
y = 2
if x > 0 or y > 5:
    print("At least one condition is True")
else:
    print("Both conditions are False")

or → at least one True

42
New cards

len(variable)

to get the length of the characters

43
New cards

{ } curly brackets

If you want to add a variable in an f string, what do we put?