Python Basics

Python Basics

Python Basics

A Byte of Python

Python Basics

http://gg.gg/1b7cu8

A Byte of Python

Python Basics

Comments

  • Use # for comments
  • They can be above:
    # Note that print is a function 
    print('hello world') 
    
  • Or at the end
    print('hello world')  # Note that print is a function
    
Python Basics

Literal Constants

  • Numbers: 5, 1.23
  • Strings: 'This is a string', "It's a string!"
  • Boolean: True, False
  • Nothing!: None
Python Basics

Numbers

Numbers are mainly of two types - integers and floats.

Integers

  • An example of an integer is 2 which is just a whole number.

Floats

  • Examples of floating point numbers ("floats") are 3.23 and 52.3e-4.
  • The e notation indicates powers of 10. 52.3e-4 => 52.3 * 10^-4^.
Python Basics

Strings

  • Strings are used to represent text.
  • You can use single quotes, double quotes, or triple quotes for multi-line strings.
    'Hello'
    "Hello"
    '''
    This is a
    multi-line string
    '''
    
Python Basics

Strings (continued)

  • Strings are immutable (more on that later)
  • Use the format method:
    name = 'Swaroop'
    age = 20
    print(f'{name} was {age} years old')
    
Python Basics

Escape Sequences

  • Use \ for escape sequences
  • Examples:
    • \' for single quote
    • \" for double quote
    • \\ for backslash
    • \n for newline
    • \t for tab
Python Basics

Escape Sequences

>>> print("foo")
foo
>>> print("foo\nbar")
foo
bar
>>> print("foo\n\nbar")
foo

bar
Python Basics

Variables

  • Variables store information
    >> i = 5
    >> print(i)
    5
    >> i = i + 1
    >> print(i)
    6
    
Python Basics

Variables Rules

  • The first character of the identifier must be a letter of the alphabet or an underscore (_).
  • The rest of the identifier name can consist of letters (uppercase ASCII or lowercase ASCII or Unicode character), underscores (_) or digits (0-9).
  • Identifier names are case-sensitive. For example, myname and myName are not the same.
Python Basics

Variable Name Examples

  • age, name, foo, bar
  • age2, name_2, foo_bar
  • _age, _name - starting with _ denotes "private"
  • AGE, NAME - ALL_CAPS denotes "constant"
  • i, j, k - common for iterators in loops (more on this later)
  • df, x, y, X, Y - bad variables but common in data science
Python Basics

Variables Contain Both Data and Types

>>> i = 5
>>> type(i)
<class 'int'>
>>> i = 'foo'
>>> type(i)
<class 'str'>
Python Basics

Logical and Physical Lines

  • Logical line: single statement
  • Physical line: what you see in the editor
i = 5
print(i)
i = 5; print(i)
Python Basics

Indentation

  • Whitespace at the beginning of the line
  • Determines grouping of statements
  • Use four spaces for indentation
    if True:
        print('True')
    else:
        print('False')
    
Python Basics

Exercise: Variables

  1. Assign your name and age to variables.
  2. Calculate how many years it will be until you are 100 years old in a new variable.
  3. Print your name, your age, and the number of years until you are 100.