Week 2
Week 2
In Python no (not always) need to store source code in a file → INTERPRETED !!!!
C ++ Compilation
C++ actually adds an extra step to the basic compilation process: the code is run through
a preprocessor, which applies some modifications to the source code, before being fed to
the compiler.
This is the syntax for outputting some piece of text to the screen
Basics
• A statement is a unit of code that does something –a basic building
block of a program.
• Python uses indentation for blocks, instead of curly braces. Both tabs and spaces are supported,
but the standard indentation requires standard Python code to use four spaces.
return 0;
}
Basics: Input
Basics: Input
Constants
Fixed values such as numbers, letters, and strings, are called “constants” because their value does
not change
• Python: String constants use single quotes (‘) or double quotes (")
• C++: String constant use double quotes ("), characters use single quotes (')
#include <iostream>
>>> print(123)
123 using namespace std;
>>> print(98.6)
98.6 int main()
>>> print('Hello world') {
Hello world cout << 123<<endl;
cout << 98.6<<endl;
cout<<" Hello world";
return 0;
}
Reserved Words
C++
Python
asm double new switch
False class return is finally auto else operator template
None if for lambda continue
True def from while nonlocal break enum private this
and del global not with case extern protected throw
as elif try or yield catch float public try
assert else import pass
char for register typedef
break except in raise
class friend return union
const goto short unsigned
continue if signed virtual
default inline sizeof void
delete int static volatile
do long struct while
Data types
• Every expression has a type – a formal description of what kind of
data its value is.
• For instance,0 is an integer, 3.142 is a floating-point (decimal)number
int main()
{ int x;
x=8;
cout << x<<endl;
return 0;
}
Type Matters >>> eee = 'hello ' + 'there'
>>> eee = eee + 1
Traceback (most recent call last):
File "<stdin>", line 1, in
• Python knows what “type” <module>TypeError: Can't convert
everything is 'int' object to str implicitly
>>> b = 20
>>> a + b
30
adopted from https://www.geeksforgeeks.org
C ++ operators!!
(new and delete)
In following weeks, we will
learn the details on new and
delete operators!!
Python: Assignment Operators
Operator Description Syntax
= Assign value of right side of expression to left side operand x=y+z
Add and Assign: Add right side operand with left side operand and then assign to left
+= a += b
operand
Subtract AND: Subtract right operand from left operand and then assign to left
-= a -= b
operand: True if both operands are equal
*= Multiply AND: Multiply right operand with left operand and then assign to left operand a *= b
/= Divide AND: Divide left operand with right operand and then assign to left operand a /= b
Modulus AND: Takes modulus using left and right operands and assign result to left
%= a %= b
operand
Divide(floor) AND: Divide left operand with right operand and then assign the
//= a //= b
value(floor) to left operand
Exponent AND: Calculate exponent(raise power) value using operands and assign value
**= a **= b
to left operand
&= Performs Bitwise AND on operands and assign value to left operand a &= b
|= Performs Bitwise OR on operands and assign value to left operand a |= b
^= Performs Bitwise xOR on operands and assign value to left operand a ^= b
>>= Performs Bitwise right shift on operands and assign value to left operand a >>= b
<<= Performs Bitwise left shift on operands and assign value to left operand
Assignment Statements
• We assign a value to a variable using the assignment statement (=)
x = 3.9 * x * ( 1 - x )
0.6 0.6
x = 3.9 * x * ( 1 - x )
0.4
0.4
The right side is an expression. Once the
expression is evaluated, the result is
placed in (assigned to) the variable on the
0.936
left side (i.e., x).
>>> x+=[1]
>>> x
[1 2 3 1]
Assignment Examples in C++
Arithmetic Operators: Python
>>> a = 4
>>> b = 3
>>> -b
-3
>>> a + b
7
>>> a / b
1.3333333333333333
>>> a % b
1
>>> a ** b
64
>>> 10 / 5
2.0
>>> type(10 / 5)
<class 'float’>
>>> 2 * 25 + 30
80
Comparison Operators : Python
>>> a = 10
>>> b = 20
>>> a == b
False
>>> a != b
True
>>> a <= b
True
>>> a >= b
False
>>> a = 10
>>> b = 10
>>> a == b
True
>>> type(a==b)
<class 'bool’>
More on Boolean >>> type([])
>>> print(bool(0), bool(0.0), bool(0.0+0j)) <class 'list’>
False False False
>>> bool([])
>>> print(bool(-3), bool(3.14159), bool(1.0+1j)) False
True True True
>>> type([1, 2, 3])
>>>type('') <class 'list’>
<class 'str’>
>>> bool([1, 2, 3])
>>>type('''')
True
<class 'str’>
>>>x=[1, 2, 3]
>>> x
>>> print(bool(''), bool(""))
[ 1 2 3]
False False
>>>x+=[1]
>>> print(bool('foo'), bool("fooooo")) >>> x
True True [1 2 3 1]
>>> x = 5
>>> x = 3
>>> y = 4
>>> x or y
3
>>> x = 0.0
>>> y = 4.4
>>> x or y
4.4