CS DS 1100 Variables and Expressions Week2 Annotated
CS DS 1100 Variables and Expressions Week2 Annotated
Discrete Structures
Course Introduction
CS 2212
CS/DS 1100
DISCRETE STRUCTURE
Applied Programming and
Problem Solving with Python
Lecture-Week 02 A
Identifier
Python Identifier
Identifier
is a name used in programming languages
Identifier
to identify a
variable
function
module
Identifier: Naming rules
Alphabetic Characters:
- Must begin with a letter (either uppercase or lowercase)
or an underscore (_).
Identifier
Digits:
- After the first character, identifiers can include digits (0-9)
Underscore:
- May contain underscores
- Used to separate words
Case Sensitivity:
- Case-sensitive in most programming languages
- myName and myname are different identifiers
Reserved Keywords:
- Cannot be the same as reserved keywords
Naming Convention
camelCase
PascalCase
snake_case
Naming Convention
camelCase First letter of the first word is lowercase, and the first
letter of each subsequent word is capitalized
Languange: JavaScript and Java
my_name sum
Variables
student_name True
student_gpa list
studentGpa
False
studentScore
None
Valid Variable Names Invalid Variable Names
my_name sum
Variables
Python Statement
Write a program to calculate a student’s total average score based on
the following information.
exam1 = 90 #15%
Python code
exam2 = 95 #15%
finalExam = 93 #25%
paScore = 98 #25%
zyScore = 90 #10%
tophatScore = 90 #10%
Comments
Multi-line '''Initial temperature
comment in Celsius
'''
celsius = 28.5
Formatting
Single-line
# Convert Celsius to Fahrenheit
comment
fahrenheit = (celsius * 9/5) + 32
print('Temperature in Fahrenheit is:' + str(fahrenheit))
print(f"Temperature in Fahrenheit is: {fahrenheit:.1f} F")
Formatted Print
# Initial temperature in Celsius
celsius = 28.5
Arithmetic Expressions
Arithmetic Expressions
float is a data type for floating-point numbers.
Two digits after the decimal point print(f'{myFloat:.2f}')
Arithmetic Operator/Convention
Description
operator ()
+ addition
exponent **
- subtraction
unary -
* multiplication
*/%
/ division
+-
x ** y (x to the power of
**
y) left-to-right
Compound Operators
Compound Operators
Compound Operators
X = 1 / 2 # Evaluates to 0.5
X = 1 / 2.0 # Evaluates to 0.5
X = 1.0 / 2 # Evaluates to 0.5
X = 1.0 / 2.0 # Evaluates to 0.5
Implicit conversion
A = 1
B = 2
X = A / B # Evaluates to 0.5
• Type conversion that is implicit
Implicit and Explicit Type Conversions
• The conversion is done automatically by the interpreter
Explicit conversion
an_int = int(7.8)
a_float = float(7) OUTPUT
7
print(an_int)
7.0
print(a_float)
12 3
Input()
input() function
Get input from user
How old are you?
User’s Input
years_to_vote = 18 - age
print(f"You are {years_to_vote} years away from
being eligible to vote in the USA.")
The round Function
print(round(3.955555,2))
print(round(-3.1))
print(round(3.1))
print(round(-3.9))
3.96
-3
3
-4
Math Module
import math
Calling math Functions
import math
square_root = math.sqrt(121.0)
print(square_root) The math.fabs() method returns
the absolute value of a number, as
a float
print(math.fabs(-50) * 2)
11.0
100.0
Number representation and theoretic functions
The math Module
import math
floor_pos = math.floor(3.9) # 3
floor_neg = math.floor(-3.1) # -4
import math
floor_pos = math.ceil(3.1) # 4
−∞ 0 +∞
floor_neg = math.ceil(-3.9) # -3
pcwallart.com
This Photo by Unknown Author is licensed under CC BY-NC This Photo by Unknown Author is licensed under CC BY-NC