0% found this document useful (0 votes)
1 views

2_Variables and simple data types

Chapter 1 covers the basics of Python, focusing on variables, data types, and user input. It explains variable naming conventions, the use of strings, numbers, and booleans, as well as methods for manipulating these data types. Additionally, it introduces error handling and the philosophy of Python through 'The Zen of Python'.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

2_Variables and simple data types

Chapter 1 covers the basics of Python, focusing on variables, data types, and user input. It explains variable naming conventions, the use of strings, numbers, and booleans, as well as methods for manipulating these data types. Additionally, it introduces error handling and the philosophy of Python through 'The Zen of Python'.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Chapter 1: Python basic

Lecturer: Nguyen Tuan Long, Phd


Email: [email protected]
Mobile: 0982 746 235
Chapter 1: Python basic 2

1.2. Variables and fundamental data types


• Variables
• String
• Numbers
• Boolean
• Checking the type and Converting Data Types
• Get user input
Variables 3

What happens when you run this code

Variables are used to store values


Variables 4

Naming and Using Variables


A few rules:
• Variable names can contain only letters, numbers, and underscores.
• Spaces are not allowed in variable names, but underscores can be used
to separate words in variable names.
• Avoid using Python keywords and function names as variable names;
that is, do not use words that Python has reserved for a particular
programmatic purpose, such as the word print.
• Variable names should be short but descriptive.
• Be careful when using the lowercase letter l and the uppercase letter O
because they could be confused with the numbers 1 and 0.
Variables 5

NOTE The Python variables you’re using at this point should


be lowercase. You won’t get errors if you use
uppercase letters, but uppercase letters in variable
names have special meanings that we’ll discuss in
later chapters.
Avoiding Name Errors When Using Variables
Variables 6

Variables Are Labels

• Variables are often described as boxes you can store values in


• It’s much better to think of variables as labels that you can assign to
values .
x y z
“Hello
61 True “DSEB” 62
Python”

x = “Hello Python” y = 61 z = True x = “DSEB” y += 1


Practice 7

2-1, 2-2
Strings 8

A string is a series of characters. Anything inside quotes (single or double


) is considered a string in Python

Example:

“This is a string”

‘This is also a string’

'I told my friend, "Python is my favorite language!“’

"The language 'Python' is named after Monty Python, not the snake.“

"One of Python's strengths is its diverse and supportive community."


Strings 9

Changing Case in a String with Methods


Strings 10

Using Variables in Strings


Strings 11

Adding Whitespace to Strings with Tabs or Newlines

• \n: newlines
• \t: tabs
Strings 12

Stripping Whitespace

• rstrip():remove the extra


space from the right side

• lstrip(): remove the extra


space from the left side

• strip(): remove the extra


space from both sides
Strings 13

Avoiding Syntax Errors with Strings


Practice 14

2-3,…, 2.7
Numbers 15

Integers Floats Integers and Floats


 2+3

 20 mod 3

 [203]
Numbers 16

Underscores in Numbers Constants


• A constant is like a variable whose
value stays the same throughout
the life of a program
• Python doesn’t have built-in
constant types, but Python
programmers use all capital letters
to indicate a variable should be
Multiple Assignment treated as a constant and never be
changed
Boolean 17
Booleans is a binary variable, having two possible values True or False.

Logical Operator

• Logical not: True if operand is false

• Logical and: True if both the operands are


true

• Logical or: True if either of the operands is


true
Checking the type and Converting Data Types 18

type() str() int() float()


Practice 19

2-8, 2-9
Get user input 20

input() function:
• Like the print() function, input will print the string inside of the
parenthesis.
• But it will also create a box for the user to enter information.

Storing User Input


Get user input 21

Converting User Input

Handling Errors : try and except blocks


Comment 22

How Do You Write Comments?

What Kind of Comments


Should You Write?
The Zen of Python 23

The Python community’s philosophy is contained in “The Zen of Python” by


Tim Peters.

You might also like