0% found this document useful (0 votes)
2 views

Lecture 02 - Intro to Python 2

This document serves as an introduction to Python programming, covering key features such as variables, data types, and basic syntax. It discusses the use of various Integrated Development Environments (IDEs), the importance of coding style, and how to handle user input and decision-making with if statements. Additionally, it highlights Python's dynamic typing and object management principles.

Uploaded by

thqgphub
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 02 - Intro to Python 2

This document serves as an introduction to Python programming, covering key features such as variables, data types, and basic syntax. It discusses the use of various Integrated Development Environments (IDEs), the importance of coding style, and how to handle user input and decision-making with if statements. Additionally, it highlights Python's dynamic typing and object management principles.

Uploaded by

thqgphub
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Fundamentals of

Programming
Simple Python Programming

1
Overview
●Introduce Python programming
● Pre se nt e xample s illustrating ke y language fe ature s

2
IDEs

●Spyde r (which come s with Anaconda),


● PyCharm
● Visual Studio Code
● Colab note books

3
Statements

●Each state me nt spe cifie s a task to pe rform.

4
Variables

●Variable s are use d to store values


assignme nt state me nt

7 is a value

x is a variable

assignme nt symbol (=)

“x is assigned the value 7”


5
Calculations in Assignment Statement
x and y are operands
+ is an addition operator

6
Python Style

●The Style Guide for Python Code


(https://www.python.org/de v/pe ps/pe p-0 0 0 8/) he lps you
write code that conforms to Python’s coding conve ntions.
○ The style guide re comme nds inse rting one space on e ach side of the
assignme nt symbol = and binary ope rators like + to make programs
more re adable .

7
Variable names

●A variable name , such as x, is an identifier .


● Each ide ntifie r may consist of
○ le tte rs,
○ digits
○ and unde rscore s (_)
○ but may not be gin with a digit.
● Python is case sensitive

8
Types

type is a built -in function

A function performs a task when you call it by writing its name, followed by
parentheses, (). The parentheses contain the function’s argument—the data that the
type function needs to perform its task.

9
Arithmetic

10
Exception and Trackbacks

11
Grouping expressions with parentheses

12
Operator precedence rules

https://docs.python.org/3/reference/expressions.html#operat
or-pre ce de nce

13
Operand types

●If both ope rands are inte ge rs, the re sult is an inte ge r—
e xce pt for the true -division (/) ope rator, which always
yie lds a floating-point numbe r.
● If both ope rands are floating-point numbe rs, the re sult is a
floating-point numbe r.
● Expre ssions containing an inte ge r and a floating- point
numbe r are mixed-type expressions —the se always
produce floating-point numbe rs.

14
Function print and an Intro to Single- and
Double- Quoted Strings

When a backslash (\) appears in a string, it’s known as the escape character.

\n represents the newline character escape sequence, which tells print to move
the output cursor to the next line. 15
Python programmers
generally prefer
single quotes.

16
Other Escape Sequences

17
Printing the Value of an Expression

18
Ignoring a Line Break in a Long String

You may also split a long string (or a long statement) over
se ve ral line s by using the \ continuation character as the
last characte r on a line to ignore the line bre ak:

19
Triple-Quoted Strings

Triple-quote d strings be gin and e nd with three double


quotes (""") or three single quotes (''') . The Style Guide for
Python Code re comme nds thre e double quote s ("""). Use
the se to cre ate :
● multiline strings,
● strings containing single or double quote s and

20
Multiline Strings

21
Getting Input from the User

The built-in input function


requests and obtains user input

22
Function input Always Returns a String

23
Getting an Integer from the User

convert the string to an


integer using the built-in
int function

24
Decision Making: The if Statement and
Comparison Operators
●A condition is a Boole an e xpre ssion with the value True
or False.
● True and False are ke ywords

25
comments

Docstrings
The Style Guide for
Python Code states that
each script should start
with a docstring that
explains the script’s
purpose

Splitting a lengthy
statement across lines

Python requires you to


indent the statements in
suites.
The Style Guide for
Python Code
recommends four-
space indents

26
Chaining Comparisons

27
Precedence of the Operators

28
Objects and Dynamic Typing

●Eve ry obje ct has a type and a value .

29
Variables Refer to Objects

●Assigning an obje ct to a variable binds (associate s) that


variable ’s name to the obje ct.

30
the variable x re fe rs to the inte ge r obje ct containing 7

31
Dynamic Typing

Python uses dynamic typing —it de te rmine s the type of the


obje ct a variable re fe rs to while e xe cuting your code .

garbage collection—helps ensure that memory is available for new


objects you create.
32
Q&A

33

You might also like