Basic Python (Part 1)
CSC1105 PROGRAMMING WITH PYTHON
Learning objectives
At the end of this chapter, student should be able to:
1. Identify the syntax and variable in python
Introduction to Python CSC1105 PROGRAMMING
WITH PYTHON
What is Python?
What can Python do?
used on a server to create web applications
can also read and modify files.
can be used to handle big data and perform complex mathematics.
Python Syntax compared to other
programming languages
• Python was designed for readability and has some similarities to the English
language with influence from mathematics.
• Python uses new lines to complete a command, as opposed to other
programming languages which often use semicolons or parentheses.
• Python relies on indentation, using whitespace, to define scope; such as the
scope of loops, functions and classes. Other programming languages often
use curly-brackets for this purpose.
Syntax CSC1105 PROGRAMMING
WITH PYTHON
What is syntax?
Syntax refers to the rules and structure that define how to write code in a specific language.
Python Indentation
Indentation refers to the spaces at the beginning of a code line.
Where in other programming languages the indentation in code is for readability
only, the indentation in Python is very important.
Python uses indentation to indicate a block of code.
Python Indentation
The number of spaces is up to you
as a programmer, the most common
use is four, but it has to be at least
one.
You have to use the same number
of spaces in the same block of code,
otherwise Python will give you an
error:
Comments CSC1105 PROGRAMMING
WITH PYTHON
Why need to use comment?
explain Python
code.
make the code
more readable.
prevent
execution when
testing code.
Creating a comment
Comments starts with a #, and Python will
ignore them
Comments can be placed at the end of a
line, and Python will ignore the rest of the
line
A comment does not have to be text that
explains the code, it can also be used to
prevent Python from executing code
Multiline comments
Python does not really have a syntax
for multiline comments. To add a
multiline comment, you could insert a #
for each line:
add a multiline string (triple quotes)
Variables CSC1105 PROGRAMMING
WITH PYTHON
What is variables?
Creating Variables
Python has no command for declaring a variable.
A variable is created the moment you first assign a
value to it.
Variables do not need to be declared with any
particular type, and can even change type after
they have been set.
Casting
If you want to specify the data type of a variable, this can be done with casting.
Get the type
You can get the data type of a variable with the type() function.
Variable Names
1. A variable can have a short name (like x and y) or a more descriptive name (age, carname,
total_volume). Rules for Python variables:
2. A variable name must start with a letter or the underscore character
3. A variable name cannot start with a number
4. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
5. Variable names are case-sensitive (age, Age and AGE are three different variables)
6. A variable name cannot be any of the Python keywords.
Legal VS Illegal variable names
LEGAL ILLEGAL
Multi words variable Names
Multi words variable Names
Multi words variable Names
Assign Multiple Values
MANY VALUES TO MULTIPLE VARIABLES ONE VALUE TO MULTIPLE VARIABLES
Unpack a collection
Output Variables
SIMPLE MULTIPLE VARIABLES
Output Variables
+ OPERATOR NUMBER WITH + OPERATOR
Output Variables
+ OPERATOR
COMBINE STRING AND NUMBER
TRUE OR FALSE?
COMMA
COMBINE STRING AND NUMBER
TRUE OR FALSE?
Conclusions
What have you learned today?

basic python part 1_powerpoint slideshare

  • 1.
    Basic Python (Part1) CSC1105 PROGRAMMING WITH PYTHON
  • 2.
    Learning objectives At theend of this chapter, student should be able to: 1. Identify the syntax and variable in python
  • 3.
    Introduction to PythonCSC1105 PROGRAMMING WITH PYTHON
  • 4.
  • 5.
    What can Pythondo? used on a server to create web applications can also read and modify files. can be used to handle big data and perform complex mathematics.
  • 6.
    Python Syntax comparedto other programming languages • Python was designed for readability and has some similarities to the English language with influence from mathematics. • Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses. • Python relies on indentation, using whitespace, to define scope; such as the scope of loops, functions and classes. Other programming languages often use curly-brackets for this purpose.
  • 7.
  • 8.
    What is syntax? Syntaxrefers to the rules and structure that define how to write code in a specific language.
  • 9.
    Python Indentation Indentation refersto the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important. Python uses indentation to indicate a block of code.
  • 10.
    Python Indentation The numberof spaces is up to you as a programmer, the most common use is four, but it has to be at least one. You have to use the same number of spaces in the same block of code, otherwise Python will give you an error:
  • 11.
  • 12.
    Why need touse comment? explain Python code. make the code more readable. prevent execution when testing code.
  • 13.
    Creating a comment Commentsstarts with a #, and Python will ignore them Comments can be placed at the end of a line, and Python will ignore the rest of the line A comment does not have to be text that explains the code, it can also be used to prevent Python from executing code
  • 14.
    Multiline comments Python doesnot really have a syntax for multiline comments. To add a multiline comment, you could insert a # for each line: add a multiline string (triple quotes)
  • 15.
  • 16.
  • 17.
    Creating Variables Python hasno command for declaring a variable. A variable is created the moment you first assign a value to it. Variables do not need to be declared with any particular type, and can even change type after they have been set.
  • 18.
    Casting If you wantto specify the data type of a variable, this can be done with casting.
  • 19.
    Get the type Youcan get the data type of a variable with the type() function.
  • 20.
    Variable Names 1. Avariable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for Python variables: 2. A variable name must start with a letter or the underscore character 3. A variable name cannot start with a number 4. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) 5. Variable names are case-sensitive (age, Age and AGE are three different variables) 6. A variable name cannot be any of the Python keywords.
  • 22.
    Legal VS Illegalvariable names LEGAL ILLEGAL
  • 23.
  • 24.
  • 25.
  • 26.
    Assign Multiple Values MANYVALUES TO MULTIPLE VARIABLES ONE VALUE TO MULTIPLE VARIABLES
  • 27.
  • 28.
  • 29.
    Output Variables + OPERATORNUMBER WITH + OPERATOR
  • 30.
    Output Variables + OPERATOR COMBINESTRING AND NUMBER TRUE OR FALSE? COMMA COMBINE STRING AND NUMBER TRUE OR FALSE?
  • 31.