Python's Basics
CSCI 1470

by K. Yue

1. Python's Interpreter

Example:

Example:

In Java, to run a program, Hello.java:

[1] "javac Hello.java": compile Hello.java into Hello.class, a Java bytecode program. Java bytecode has .class as its file extension. It is a platform independent intermediate level programming language.

[2] "java Hello": invoke the Java Virtual Machine (JVM) to run the Java bytecode program Hello.class. Note that the instruction is not "java Hello.class" as the file extension of .class is assumed.

1.1 Python Interpreter

1.1 Python IDLE

2. Python Programs

2.1 Python Literals and Variables

2.2 Python Statements

Try out statement_1.py

#    Python statements

#   A Python statement is terminated by the end of the physical line,
#   represented by the end of line character: \n.

print('hello')

print('hello again');

#   Not recommended Python style.
print('hello '); print('world');

#   the second argument (end = ' ') indicates that the end string
#   for the print statement is ' ', and not the default '\n'
print('hello ', end = ' '); print('world');

#   \n is the end of line character. \ is used here for 'escaping' a character into special characters.
print('first line\nsecond line\nthird line');

#   Because of the , character, Python knows that this
# not the end of the statement and extends it to the next line.
print('hello', 
    'world')

#   The character \ extends the statement to the next line.   
#   There should not be any character after \.
print('hello'     \
    , 'world')   
   

 2.3 Expressions, Functions, Operators and Methods

Examples:

Try out expression_ex1.py

a = 1
b = 2
c = 'hello'
print(max(a, b, 3))
print(round((a+100)/(b+5)))
max(a, b, 12, b*7, 6)
a + b * 4
a < b
a > b
a / b
d = (a + 4) ** b
e = c + ' world'
print(a, b, c, d, e)
print(e.upper())