[go: up one dir, main page]

0% found this document useful (0 votes)
40 views30 pages

Lecture 2.1 After Large

The document discusses data types and variables in Python programming. It covers core data types like integers and floats. It also discusses running Python programs through interactive mode, scripts, and integrated development environments. The document provides examples of Python code and defines key concepts like programs, debugging, strings, and values.

Uploaded by

jinho baek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views30 pages

Lecture 2.1 After Large

The document discusses data types and variables in Python programming. It covers core data types like integers and floats. It also discusses running Python programs through interactive mode, scripts, and integrated development environments. The document provides examples of Python code and defines key concepts like programs, debugging, strings, and values.

Uploaded by

jinho baek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

INTRODUCTION TO

COMPUTING SCIENCE AND PROGRAMMING


Lecture 2.1: Data Types

CMPT 120, Spring 2023, Mohammad Tayebi


Class Agenda

• Last time • Today


• Course structure • Python
• Data types
• “How to” Knowledge
• Variable
• Algorithm & Problem solving
• Operators
• Tips for doing well • Input function
• Show up, practice and enjoy

• Reading
• cspy ch. 1 & 2

CMPT 120, Spring 2023, Mohammad Tayebi 2


Topic for discussion

Do/How algorithms rule the world?

CMPT 120, Spring 2023, Mohammad Tayebi 3


High/middle/low Level Languages 1
• Machine can only understand low-level (machine) language.
• Contains only binary numbers (1 & 0).

• Middle-level (assembly) language consists of symbolic


equivalent of machine language.
• Assembler converts assembly language to machine language.
• Assembly is platform-dependent.
• Developers usually consider assembly as a low-level language

• High-level languages can be understood by users.


• Similar to human language with a set of keywords and grammar
rules (syntax).
https://www.cs.mtsu.edu/~xyang/2170/computerLanguages.html

• Programming in high-level languages is easier.


• Python, C, C++, C#, Java, Perl, Lisp, Ruby, …
CMPT 120, Spring 2023, Mohammad Tayebi 4
Code - Example
Assembly Machine Code

CMPT 120, Spring 2023, Mohammad Tayebi 5


Interpreted vs. Compiled Languages
• Programs in high-level languages should be translated to machine language.
• Compilers and interpreters

• Compilers read the source code completely and translate it to machine code
that can be executed by computer.
• C, C++, Java, …

• Interpreters read a single chunk of


source code, translate and execute it,
then fetch the next chunk.
• Python, Perl, Ruby, …
CMPT 120, Spring 2023, Mohammad Tayebi 6
Running Python Program 1
• Interactive mode
• Immediate execution
• Convenient for testing small programs

• Running script
• Save the program in a file using a text editor
• example.py
• Using interpreter to run the content of file
• > python example.py

CMPT 120, Spring 2023, Mohammad Tayebi 7


Running Python Program 2

• Using IDE (Integrated Development Environment)


• Multi-faceted software tools to facilitate programming
• Python IDEs
• IDLE
• default editor that accompanies Python
• Visual Studio Code
• Open-source, free, created by Microsoft
• PyCharm
• Full-featured and dedicated to Python
•…

CMPT 120, Spring 2023, Mohammad Tayebi 8


Running Python - 3

• Coding live in Browser


• Repl.it
• Jupyter

• Why writing code on your computer?


• Avoiding network issues
• Not to make your code public https://www.elegantthemes.com/blog/wordpress/7-best-browser-based-online-code-editors-for-web-developers

• Easier to manage large projects


• Industry standard
• …

CMPT 120, Spring 2023, Mohammad Tayebi 9


What Is a Program?
• Sequence of instructions that specifies how to perform a computation
• Solving mathematical problem, finding a word in a document, …

• A program consists of
• Input, output, arithmetic and logical operations, conditional
execution and repetition
• Programming is breaking large and/or sophisticated
tasks to subtasks
• And continuing this process until the subtasks are simple
enough to be executed with basic instructions.

CMPT 120, Spring 2023, Mohammad Tayebi 10


Debugging

• Programming errors are called bugs


• Syntax error
• grammatical error
• Runtime error
• Exception
• Sematic error
• Program will do something different.
• Debugging is the process of finding
and fixing bugs in source code. https://www.uplers.com/blog/top-5-debugging-front-end-developer-tools/

CMPT 120, Spring 2023, Mohammad Tayebi 11


'

Hello World!
Strings in Python can be
>>> print("Hello, World!") enclosed in different ways.

>>> print("""Hello, World!""") Hello World!

>>> print(' ' ' Hello, World! ' ' ')

>>> print(' ' ' Hello, World! ' ' ')

>>> print("Bruce's beard") Bruce's beard


Double quoted strings can contain single
quotes inside them, and vice versa.
>>> print(‘"Excellent"') " Excellent"
CMPT 120, Spring 2023, Mohammad Tayebi 12
In-class Exercise 1

• What is the output of the following expression?

>>> print("""H2O’’SO2+/9""")

1. H2OSO2+/9
2. ""H2O’’SO2+/9""
3. "H2O’’SO2+/9"
4. H2O’’SO2+/9

CMPT 120, Spring 2023, Mohammad Tayebi 13


Values & Data Types

• We pass values (objects) to programs as


input and they manipulate values to
produce results.
• Numbers, words, list of numbers, words, …
• Values in each programming language are
classified to different data types (classes).

https://www.linkedin.com/pulse/data-types-variables-expressions-python-anubhav-kumar/

CMPT 120, Spring 2023, Mohammad Tayebi 14


Core Data Types
• Integer
• x = 20
• Float Function type is used to know type of
• x = 1.2 values/variables.
• Strings
• x = "Hi" >>> print(type(12))
• Boolean <class 'int'>
• x = True
>>> print(type("12"))
• Lists
• x = ["apple", "banana", "cherry"] <class 'str'>
• Tuples >>> print(type(12.15))
• x = ("apple", "banana", "cherry")
<class 'float'>
• Sets
• x = {"apple", "banana", "cherry"}
• Dictionaries
• x = {"Nepal": "Kathmandu", "Italy": "Rome", "England": "London"}
CMPT 120, Spring 2023, Mohammad Tayebi 15
Converting Data Types

• Sometimes we need to convert values from one type to another


• int()
• str() >>> print (3.73)
• float() 3.73
>>> print (str(3.73))
3.73
>>> print (int(3.73))
3
>>> >>> print(int("12"))
12
>>> print(int("12abc"))
Error
CMPT 120, Spring 2023, Mohammad Tayebi 16
Variables

• A variable is a named value that references or stores a piece of data.


• You can refer a variable many times in a program.
• The value of variable may change during the program.

• Assignment statements, =, create new


variables and give them values to refer to.
>>> message = "What's up, Doc?"
 n is assigned 17 >>> n = 17
× n equals 17 >>> pi = 3.14159

CMPT 120, Spring 2023, Mohammad Tayebi 17


Variable Names and Keywords
• Variable names
• Arbitrary long
Python keywords can not be
• Never contain spaces used as variable names.
• Contain letters, digits and underscore
and is break not
• Begin with letter or underscore
def raise else while
• You can use uppercase letters
finally True global continue
• Avoid it as a convention
• Note that max and Max are two different variable in assert nonlocal exec
pass elif try import
yield from None or
 Using meaningful variable names make the code as lambda class with
easier to read, understand and maintain. del return except
for False if
CMPT 120, Spring 2023, Mohammad Tayebi 18
In-class Exercise 2

• Assume you have defined a variable called y in a python program with


integer value. Now, which expression has wrong syntax?

1. print(y)
2. print(y,y)
3. print(y+1)
4. Print(y+y)
5. Print(yy)
6. Options 2 & 3 & 4 & 5
7. None of the options
CMPT 120, Spring 2023, Mohammad Tayebi 19
In-class Exercise 3

• Which variable name in a python program will not


cause a syntax error?
1. 1zyzzyzzyzzyzzyzzyz
2. _x?yx
3. xyz!
4. xyz=1z
5. while1
6. xy#z
7. None of above

CMPT 120, Spring 2023, Mohammad Tayebi 20


Operators 1

• Operators are special tokens that represent computations.


• The values the operator works on are called operands.

• Arithmetic operators: + - * % ** / //
• % (mode or modulo) is remainder (11 % 3 is 2)
• Modding by 0 is an error, just like dividing by 0.
• ** is exponentiation (10**2 is 100)
• / is floating point division (3/2 is 1.5)
• // is integer division (3//2 is rounded down to 1).
CMPT 120, Spring 2023, Mohammad Tayebi 21
Operators 2
• Comparison operators: < <= == != >= >
• == tests if two values are equal
• != tests if two values are not equal
• 3 != 2 will be True, 3 == 2 will be False.

• Logical operators: and or not

• Assignment operators += -= *= /= //= %= **=


• Each of these is shorthand. e.g., x += 5 is a shorthand for x = x + 5

CMPT 120, Spring 2023, Mohammad Tayebi 22


Reassignment and Updating Variable

• You can assign values to the same variable multiple times.

>>> x = 3
>>> Y = 2
>>> x = x * y
>>> x = x + 1
>>> print (x)
7

CMPT 120, Spring 2023, Mohammad Tayebi 23


In-class Exercise 4

• What is the output of the following program?


>>> a = 4
>>> b = 6
>>> c = a + b
>>> a = a + 2
>>> c -= 5
>>> b = a * c
>>> print(b) 1. 10
2. 20
3. 30
4. 40
CMPT 120, Spring 2023, Mohammad Tayebi 24
In-class Exercise 5

• What is the output of the following program?

>>> a = 8
>>> b = 2
>>> c = a // b
>>> b = b ** b
>>> c += b
>>> print(c+1) 1. 9
2. 10
3. 11
4. 12
CMPT 120, Spring 2023, Mohammad Tayebi 25
In-class Exercise 6

• What is the output of the following program?

>>> a = 1
>>> b = 2
>>> c = 1/2
>>> d = a + 1
>>> d = 12 / c
>>> print(d) 1. 21
2. 22
3. 23
4. 24
CMPT 120, Spring 2023, Mohammad Tayebi 26
In-class Exercise 7

• What is the output of the following program?

a = 10
b = 30
c = a//b
d = b/c
e = a + b + c + d
print(e) 1. 40
2. 43
3. 44
4. Error
CMPT 120, Spring 2023, Mohammad Tayebi 27
Order of Operations

• Rules of precedence in case of having more than one operator in an expression


1. Inside of parenthesis
2. Exponentiation
3. Multiplication/division/remainder
4. Addition/subtraction
5. Assignment

• String constants
• Anything inside of single quotes or double quotes, and + for strings is concatenation

>>> print("What " + "is " + "your " + "name?")


What is your name?
CMPT 120, Spring 2023, Mohammad Tayebi 28
Reading User Input

• The input() function take the user's input


• Return it as a string value.
• Even if you ask for a number input() returns a string.
• Your responsibility to convert it.

name = input (“Enter your first name: ")

CMPT 120, Spring 2023, Mohammad Tayebi 29


Next Lecture

Data Types - Continue


Pre-reading: N/A

CMPT 120, Spring 2023, Mohammad Tayebi


30

You might also like