Programming Fundamentals Lecture 02
Programming Fundamentals Lecture 02
Fundamentals
MPW 224
Lecture 02
Dr. Mohamed Elsabahy
Ph.D. in Energy Resource Engineering
Email: Mohamed_Elsabahy@eng.suez.edu.eg
Working with Variables in Python to
Manage Data
Getting Started With Python Lecture Content
`print()` Function
`print()` Function
- The `print()` function is used to output text (or other data) to the console or standard output device.
- It takes zero or more parameters as input and displays them on the console, separated by spaces by
default.
- It is commonly used for debugging, displaying information to users, or any other form of output.
- Syntax: `print(value1, value2, ..., sep=' ', end='\n', file=sys.stdout, flush=False)`
- `value1`, `value2`, ...: Any number of values or expressions to be printed.
- `sep`: Specifies the separator between the values. By default, it is a space `' '`.
- `end`: Specifies what to print at the end. By default, it is a newline `'\n'`.
- `file`: Specifies the output file. By default, it is the console (`sys.stdout`).
- `flush`: If `True`, the output buffer is flushed after printing. Default is `False`.
Getting Started With Python
Working with Variables in Python to Manage Data
Example
Getting Started With Python
Working with Variables in Python to Manage Data
• You can check the type of a variable using the type() function.
print(type(x)) # <class 'int’>
print(type(y)) # <class 'float’>
print(type(name)) # <class 'str'>
Variable Reassignment
• You can change the value of a variable by assigning it a new value.
x=5
x=x+1
• After executing these statements, the value of x will be 6.
Getting Started With Python
Working with Variables in Python to Manage Data
Python Variables
Multiple Assignment
• Python allows you to assign values to multiple variables in a single line.
a, b, c = 10, 20, 30
• This assigns 10 to a, 20 to b, and 30 to c.
Constants
• Although Python doesn't have built-in constant types, programmers often use variables with
uppercase names to denote constants.
PI = 3.14159
• By convention, constants are usually written in uppercase to distinguish them from regular
variables.
Getting Started With Python
Working with Variables in Python to Manage Data
Python Comments
What is Comment
• Comments are used to annotate code and provide explanations or documentation.
• Comments are ignored by the Python interpreter and are not executed as part of the program.
• They are purely for the benefit of developers reading and maintaining the code
Single-line Comments
• Single-line comments begin with the # symbol and continue until the end of the line.
• They are typically used for short explanations or annotations on specific lines of code.
Multi-line Comments
• Python does not have a specific syntax for multi-line comments like some other programming
languages.
• However, you can use multiple single-line comments or a string literal (triple quotes) to achieve a
similar effect.
• Multi-line comments are useful for longer explanations or documentation.
Getting Started With Python
Working with Variables in Python to Manage Data
Python Comments
Commenting Out Code
• Comments are also commonly used to temporarily disable or "comment out" lines of code during
development or debugging.
• This allows you to exclude specific lines from execution without deleting them.
Getting Started With Python
Working with Variables in Python to Manage Data
Python Comments
Example
Questions